How to capture a dbgrid Row click event?

Asked

Viewed 2,187 times

2

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

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

  • but what action you want to perform?

  • Suppose the rows returned outside: 'id name age 1 Joao 23 2 josé 34 3 maria 43' , then I want to click on josé and get your id to compare in a case

2 answers

1


Look for the event onClick on the pick of Object Inspector.

By clicking on the Grid it already arrow in the database record, then you can capture the id and take the test.

procedure Tfrm.DBGri1Click(Sender: TObject);
begin
  case  TQuery.fieldbyname('id').asInteger of
  1:;
  2:;
  end;
end;
  • Assuming the returned lines were:

  • 1

    Supposing the rows returned outside: id name age 1 Joao 23 2 josé 34 3 maria 43 ai I want to click on josé and get its id to compare in a case

  • connected on which component of database?

  • 1

    In a Tquery with a Datasource

  • why when clicking on the Grid it already arrow in the database record, then Voce can capture the id and do the test.

  • I’ll edit my answer

Show 1 more comment

1

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

Take 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;
  • Yes, but this method would be more appropriate if you take advantage of Column: Tcolumn if you don’t mind.

  • 1

    @Gabriellocalhost Yes, for example if you want to get the selected value independently of the column: ShowMessage(Column.Field.AsString).

  • 1

    That’s right, I usually wear it anyway

Browser other questions tagged

You are not signed in. Login or sign up in order to post.