Author: Alex Schlecht
StringGrids / DBGrids with colored cells looks very nice and you can inform the
user about importent data inside the Grid.
Answer:
Unfortunately you can't use the same method for coloring StringGrids and DBGrids.
So first let's have a look to the StringGrid:
1. StringGrid
Use the "OnDrawCell"-event to make your StringGrids colorful! The following Code
shows how to give your Grid a red background color. The second column will be
colored with green background.
1 2 procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
3 Rect: TRect; State: TGridDrawState);
4 5 const//define your color here. Of course you6 //can use default colors too.7 clPaleGreen = TColor($CCFFCC);
8 clPaleRed = TColor($CCCCFF);
9 10 begin11 12 //Does the cell have the focus you have to paint it with other colors13 if (gdFocused in State) then14 begin15 StringGrid1.Canvas.Brush.Color := clBlack;
16 StringGrid1.Canvas.Font.Color := clWhite;
17 end18 else//Does the cell have NOT the focus you can use19 //your personal colors here20 if ACol = 2 //the second Column should be21 {//green, the other cells red }then22 StringGrid1.Canvas.Brush.color := clPaleGreen
23 else24 StringGrid1.canvas.brush.Color := clPaleRed;
25 26 //Now Paint the cells, but only, if the cell isn't the Title- Row/Column27 //This of course depends whether you have title-Row/Columns or not.28 29 if (ACol > 0) and (ARow > 0) then30 begin31 //Painting the Background32 StringGrid1.canvas.fillRect(Rect);
33 34 //Painting the Text. Here you can improve the code with35 // using alignment and so on.36 StringGrid1.canvas.TextOut(Rect.Left, Rect.Top, StringGrid1.Cells[ACol, ARow]);
37 end;
38 end;
If you want to colorize your cells depending on values in the cells you can replace
the 3 lines (if Acol = 2 ......) with something like this
39 if StringGrid1.Cells[ACol, ARow] = 'highlight it' then40 StringGrid1.Canvas.Brush.color := clPalered
41 else42 StringGrid1.canvas.brush.Color := clwhite;
But now lets coloring DBGrids:
2. DBGrid
It's much easier to give color to DBGrids. Here you have to use the
"OnDrawColumnCell"-Event. The following example is coloring the Cells of Column
"Status" when the value is not "a".
If you want to color the whole line you only have to delete the "If..." statement
(see below)
43 44 procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
45 DataCol: Integer; Column: TColumn;
46 State: TGridDrawState);
47 const48 clPaleGreen = TColor($CCFFCC);
49 clPaleRed = TColor($CCCCFF);
50 begin51 52 if Column.FieldName = 'Status' then//Remove this line, if you want53 //to highlight the whole line54 55 if Column.Field.Dataset.FieldbyName('Status').AsString <> 'a' then56 if (gdFocused in State) {//does the cell have the focus? }then57 dbgrid1.canvas.brush.color := clBlack //focused58 else59 dbgrid1.canvas.brush.color := clPaleGreen; //not focused60 61 //Now let's paint the cell using a Default-Method:62 dbgrid1.DefaultDrawColumnCell(rect, DataCol, Column, State)
63 end;