Changing background for TDBGrid row?

1.5k views Asked by At

I want to simulate Explorer themes for TDBGrid selected row (dgRowSelect), instead of that Blueish color. How can I do that?

Here is a sample of the expected result:

enter image description here

3

There are 3 answers

4
MartynA On

When you say "simulate", I'm not clear how you're intending to choose the selected row background color, but the following should draw it in a standard TDBGrid.

procedure TForm1.FormCreate(Sender: TObject);
begin
  DBGrid1.DefaultDrawing := False;
  DBGrid1.Options := DBGrid1.Options + [dgRowSelect];
end;

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
var
  Grid : TDBGrid;
  BackColor : TColor;
begin
  Grid := Sender as TDBGrid;
  if gdSelected in State then begin
    BackColor := clYellow;  // or whatever
    Grid.Canvas.Brush.Color := BackColor;
    Grid.Canvas.Font.Color := Grid.Font.Color;
  end;
  Grid.Canvas.FillRect(Rect);
  Grid.Canvas.TextOut(Rect.Left, Rect.Top, Field.DisplayText);
end;
0
Confundir On

You can use the OnDrawColumnCell event Here's a simple example:

procedure TForm4.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if mydataSet.FieldByName('Age').AsInteger > 18 then
    DBGrid1.Canvas.Brush.Color:= clRed;
  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

Hope this helps

0
anu On

Change the "SelectedBackColor" of the TDBGrid to the color you want.