Mega Code Archive

 
Categories / Delphi / Graphic
 

How to use multi colored lines in a grid

Title: How to use multi-colored lines in a grid 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 3rd 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; // Draw condition DBGrid1.DefaultDrawDataCell(Rect,dbgrid1.Columns[datacol].Field, State); Dbgrid1.SelectedIndex := 2; // Put cursor in the 3rd column end; Best Regards