Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DBGrid get selected cell

Tags:

delphi

dbgrid

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.

like image 886
Remus Rigo Avatar asked Dec 07 '22 05:12

Remus Rigo


2 Answers

Found it.

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

like image 148
Remus Rigo Avatar answered Dec 24 '22 19:12

Remus Rigo


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.

like image 36
user757239 Avatar answered Dec 24 '22 17:12

user757239