Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use multi-colored lines in a grid

Database Programming

How to use multi-colored lines in a grid

by  svanels  Posted    (Edited  )
Sometimes we need to differentiate the records in a table based on a certain condition of a field.

In this example we use the employee table (in dbDemos), and make a division 3 salary groups. Also we will put the cursor in the 3[sup]rd[/sup] column.


Needed: a form, a table or query, dbgrid etc.

1)Set the DefaultDrawing of the DBGrid to false

2)Use the OnDrawColumnCell event and type in:


procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Table1.FieldByName('Salary').Value > 35000 then
DBGrid1.Canvas.Font.Color := clRed
else
if Table1.FieldByName('Salary').Value > 25000 then
DBGrid1.Canvas.Font.Color := clGreen;
[color blue]// Draw condition [/color]
DBGrid1.DefaultDrawDataCell(Rect,dbgrid1.Columns[datacol].Field, State);

Dbgrid1.SelectedIndex := 2;
[color blue]// Put cursor in the 3[sup]rd[/sup] column [/color]
end;


Best Regards
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top