DBGrid get selected cell
Asked Answered
A

6

5

I need to get the value of the selected cell of a DBGrid in Delphi.

I have no idea how to do it. I tried dbGrid's OnMouseMove

pt : TGridCoord;
...
pt:=dbGrid.MouseCoord(x, y);

[Edited] I can use the OnCellClick to get the value of the cell with "Column.Field.AsString", but I want to get the value from the first column when I click on any column of that row.

Affinitive answered 30/11, 2009 at 18:11 Comment(0)
A
9

Found it.

dbGrid.Fields[0].AsString gets the value of the first column of the selected row.

Affinitive answered 30/11, 2009 at 18:52 Comment(1)
I was looking for it for about an hour and couldn't find. Thanks!Eczema
R
3
procedure TForm1.DBGrid_DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumnEh;  State: TGridDrawState);
const defaultCheckBoxFieldNumber = 1;
begin
  if DBGrid.SelectedField.FieldNo = defaultCheckBoxFieldNumber then
    ....;
  else
    ...;
end;

DBGrid.SelectedField.FieldNo gets selected field on event DrawColumnCell in TDBGrid.

Reprisal answered 17/5, 2011 at 11:13 Comment(0)
C
2

I know this is late and not sure if it is what the title means. But if it means to get the selected cell value, then try this:

procedure Form1.dbGrid1CellClick(Column: TColumn);
begin
  ShowMessage(table1.Fields[Column.Index].AsString);
end;

Make sure
dbGrid1.Options.dbRowSelect := False;

Chandelier answered 30/8, 2017 at 21:53 Comment(0)
V
1

A DBGrid has no focus, and therefore you cannot find out which row is seleted. Instead look at the linked DataSet. A DataSet has an active row.

Venusberg answered 30/11, 2009 at 18:52 Comment(0)
G
1

i think the easiest way is to connect a hidden DBText to your dataset then set the DBText to display which field you need, this way that DBText will always contain the needed value of the active record

Graph answered 1/12, 2009 at 9:15 Comment(0)
M
1

try this to get the value of selected cell in dbgrid:

procedure Form1.dbGrid1CellClick(Column: TColumn); begin ShowMessage(table1.Fields[DBGrid1.SelectedIndex].AsString); end;

Maundy answered 13/9, 2017 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.