5

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.

Community
  • 1
  • 1
Galvion
  • 1,353
  • 7
  • 23
  • 35
  • if Option `dgAlwaysShowSelection` should not be what you are looking for then the answer of the last link you posted should cover your demands. – bummi Oct 26 '13 at 07:41

5 Answers5

6

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;
No'am Newman
  • 6,395
  • 5
  • 38
  • 50
  • THIS is the best answer. It's 2021 and I'm in Delphi 10.2 (Tokyo). More on this in my (expanded answer below). – Free Dorfman Apr 05 '21 at 06:06
  • I also found this useful: http://www.delphigroups.info/2/aa/351370.html Delphi 7. Note that to get this method, go to the grid and double click on the OnDrawDataCell event. – David Lipschitz Feb 19 '22 at 11:43
3

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 :)

Galvion
  • 1,353
  • 7
  • 23
  • 35
1

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

1

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>

Free Dorfman
  • 341
  • 1
  • 3
  • 13
0

As an even further extension to No'am Newman's great answer (even in 2022), the following code will cause the selected row in a grid to be highlighted, but with respect to the current scheme using DrawCellHighlight() instead of a particular color:

[...]
 If (THackDBGrid(dbGrid1).DataLink.ActiveRecord + 1 = THackDBGrid(dbGrid1).Row)
    or (gdFocused in State) or (gdSelected in State) then
  dbGrid1.DrawCellHighlight(Rect, State, Column.Index, 0);
[...]
LuWey
  • 29
  • 4