How to capture the click event of a row of dbgrid?

2

I would like to know how to get the click event of any line, returned from the DB in GridView .

For example, GridView shows me 3 lines, I want to click on one of these lines and perform a certain action.

    
asked by anonymous 13.10.2016 / 17:03

2 answers

1

Look for the onClick event in the Object Inspector palette.

When you click on the Grid, it is already in the database record, so you can capture id and take the test.

procedure Tfrm.DBGri1Click(Sender: TObject);
begin
  case  TQuery.fieldbyname('id').asInteger of
  1:;
  2:;
  end;
end;
    
13.10.2016 / 17:06
1

Another alternative is to use the OnCellClick , it is triggered when the user clicks on some grid cell.

See an example:

procedure TForm1.DBGrid1CellClick(Column: TColumn);
var
  ID: integer;
begin
  ID := DBGrid1.Fields[0].AsInteger; // Pega a o valor da primeira coluna selecionada

  case ID of
    1: ShowMessage('João');
    2: ShowMessage('José');
    3: ShowMessage('Maria');
  else
    ShowMessage('Nome desconhecido!');
  end;
end;
    
13.10.2016 / 17:46