Question

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.

Was it helpful?

Solution 2

OK, After I tried myself, then I findout that the code below can do the code :

DBGrid1.setfocus;

Since the row already correctly selected, the setfocus help the highlight to come up.

Anyway, thanks for the replies :)

OTHER TIPS

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;

grid.SelectedRows.CurrentRowSelected := True; This helped me to highlight the current row

answered Oct 27 '13 at 5:44

IMHO, No'am Newman's answer (two above here) is THE BEST, THE CORRECT ANSWER.

It's 2021. I'm in Delphi 10.2 (Tokyo); VCL program. I've been with Delphi since version 3.02 (1986).

Some further explanation:

(1) dgAlwaysShowSelection doesn't work with dgRowSelect (and the links mentioned in this response just overcomplicate - if they work at all)

(2) DBGrid1.SetFocus moves the focus away from whatever might be the ActiveControl

(3) grid.SelectedRows.CurrentRowSelected := True; doesn't work for me

(4) No'am's hack --> it's less than 10 lines of code and adds the ability to choose a color.

(4.1) If you don't want to "choose" a color, I'd suggest clGradientActiveCaption (as opposed to No'am's clMoneyGreen).

(*) I am 98% (99%?) sure that all my points above would/will apply to any version of Delphi going back to at least 5.

<that's all I got>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top