DBGrid get selected cell

56,444

Solution 1

Found it.

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

Solution 2

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.

Solution 3

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;

Solution 4

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.

Solution 5

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

Share:
56,444
Remus Rigo
Author by

Remus Rigo

My certifications: MCTS Windows 7, Configuration MCTS Windows Server 2008 R2, Desktop Virtualization MCTS Windows Server 2008 R2, Server Virtualization MCITP Virtualization Administrator on Windows Server 2008 R2 http://about.me/remusrigo/

Updated on July 09, 2022

Comments

  • Remus Rigo
    Remus Rigo almost 2 years

    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.