Viktor Anastasov
Viktor Anastasov

Reputation: 1113

Change color of row in DBGrid after OnCellClick event

I want to change the color of the clicked row in a DBGrid in OnCellClick event and to save this state of the grid until the next OnCellClick event - than another row will be coloured etc. Does anyone know how I can achieve this ?

Upvotes: 1

Views: 2385

Answers (1)

Markus_13
Markus_13

Reputation: 328

Well, it will be rly bad decision to code rendering inside OnCellClick. Basically in OnCellClick you just simply need to save selected RecNo. Like this:

procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
  tag:=DBGrid1.SelectedField.DataSet.RecNo;
end;

And then u need to modify OnDrawDataCell func like this:

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;  Field: TField; State: TGridDrawState);
begin
  if(Field.DataSet.RecNo=tag)then begin
    DBGrid1.Canvas.Brush.Color:=clYellow;
    DBGrid1.Canvas.Font.Color:=clNavy;
  end else begin
    DBGrid1.Canvas.Brush.Color:=clWhite;
    DBGrid1.Canvas.Font.Color:=clRed;
  end;
  DBGrid1.Canvas.FillRect(Rect);
  DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;

P.S. if u have multiple entries with same RecNo, u should choose another property, which will be unical for each row.

Upvotes: 1

Related Questions