DBGrid Highlighting the located row via code?

Galvion picture Galvion · Oct 26, 2013 · Viewed 8.1k times · Source

I got the : ADOTable1 ( codepeople as integer, namepeople as string ) DataSource1 ( the DateSet is ADOTable1 ) DBGrid1 ( connected to DataSource1, Options-dgRowSelect is true )

I locate a row on ADOTable1 with the following code

  ADOTable1.Locate(ADOTable11codepeople.FieldName, 1, []);

DBGrid1 is selecting the correct row. But not with the highlights.

How to make the DBGrid automatically highlights the row that I located from ADOTable1 ?

I read the following links and did not find the answer :

How to set active cell in TDBGrid?

Delphi - Using DBGrid to select rows from a search

View position in DBGrid when scrolling in Delphi

Simple source code please...

PS: I use Delphi 2010.

Answer

No'am Newman picture No'am Newman · Oct 27, 2013

The following code will cause the selected row in a grid to be highlighted

type
 THackDBGrid = class (TDBGrid);

...

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
           const Rect: TRect; DataCol: Integer; Column: TColumn;
           State: TGridDrawState);
begin
 if (THackDBGrid(dbGrid1).DataLink.ActiveRecord + 1 = THackDBGrid(dbGrid1).Row)
  or (gdFocused in State) or (gdSelected in State) then
   dbGrid1.canvas.Brush.Color:= clMoneyGreen;


 dbGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
end;