3

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.

Jason Plank
  • 2,322
  • 4
  • 29
  • 39
Remus Rigo
  • 1,354
  • 7
  • 32
  • 56

6 Answers6

8

Found it.

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

bluish
  • 23,093
  • 23
  • 110
  • 171
Remus Rigo
  • 1,354
  • 7
  • 32
  • 56
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.

bluish
  • 23,093
  • 23
  • 110
  • 171
user757239
  • 31
  • 1
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;

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.

bluish
  • 23,093
  • 23
  • 110
  • 171
Birger
  • 4,255
  • 18
  • 32
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

Alin Sfetcu
  • 542
  • 6
  • 16
0

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

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

Asad Alamdar
  • 128
  • 8