• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 572
  • Last Modified:

DBGrid Color Change

I need that when the user enters a row in the DBGrid that row changes to a different color like 'clInfoBk'. I need the whole row and not just the Collumn Cell.
0
ramoreia
Asked:
ramoreia
  • 2
  • 2
1 Solution
 
mokuleCommented:
If You do not need dgEditing Option for dbgrid You can set
dgRowSelect to True
0
 
BdLmCommented:
dbgrid1.columns[1].color := clInfoBk;


0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Set dgRowSelect to true in Option then

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
begin
if gdselected in state then
    TDBGrid(Sender).Canvas.Brush.Color :=  clInfoBk
    else
    TDBGrid(Sender).Canvas.Brush.Color := clWindow;
  TDBGrid(Sender).DefaultDrawDataCell(Rect, Field, State);
end;
0
 
mokuleCommented:
In case You do not want to set dgRowSelect to True Try this.
Table1 is DBgrid DataSet

var
  vsel: Variant;

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if DBGrid1.DataSource.DataSet.FieldByName('YourKeyField').Value = vsel then
    DBGrid1.Canvas.Brush.Color := clRed;
  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
begin
  vsel := DataSet.FieldByName('YourKeyField').Value;
end;
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now