tcsh tcsh - 1 year ago 139
MySQL Question

DBGrid - OnCellClick & OnDblClick, return a form/TBMemo containing cell/column value/s

I'm using the following code added to my DBGrid - OnCellClick event

procedure TForm2.DBGrid1CellClick(Column: TColumn);
if dbmodule.comenziDataSet.Active then
if not Assigned(dbgridCelulaForm) then
dbgridCelulaForm := TdbgridCelulaForm.Create(Self);
dbgridCelulaForm.DBMemoCelula.DataSource := dbmodule.comenziSource;

dbgridCelulaForm.Visible := False;
dbgridCelulaForm.Visible := True;
dbgridCelulaForm.DBMemoCelula.DataField := Column.FieldName;
dbgridCelulaForm.Caption := Format('%s / randul: %d',[Column.FieldName, DBGrid1.DataSource.DataSet.RecNo]);

dbgridCelulaForm = name of the form containing the

DBMemoCelula = name of the

dbmodule.comenziDataSet = comenziDataSet is the name of the
and dbmodule is the name of a
data module
(unit, like forms) - the
is on the
data module
, so, dbmodule.comenziDataSet
dbmodule.comenziSource = same as data set, a DataSource on a data module, the source is named comenziSource

Ok so what this code does:

Once I click a cell on my
it pops up a
(named dbgridCelulaForm) which contains a
(named DBMemoCelula) and it shows me the information contained in that cell (like, a Customer Name for example, or whatever the cell is holding, in my db)

This is fine, my problem is I can't select rows now in
, well, I can but once I do the 1st place I click (a cell, any) on the particular row I want to select with my mouse, then cell activates and the
pops up.

Is it possible to use this code in
event instead of the
Meaning once i double click a row / cell the
should pop up and show me the info, but double click - not single click.

That way, I can still select the row and still view the info in the cell if I need to.

Or any other way/place to use/receive this functionality.
Any thoughts?

I can post a quick video of everything if my explanation is ambiguous and you think that would help, just tell me in the comment / answer.

Also, I'm using RAD Studio 10 Seattle and dbexpress components for the database - if that helps.


Answer Source

The following code shows how to access the Column and Row coordinates of a dbl-clicked cell of a TDBGrid, and the string value of the cell contents.

As written, it displays the cell's Column and Row number + string contents on the form's caption. Up to you what you actually do with these values.

It work because the dataset cursor on the dataset connected to the DBGrid is moved to the dataset row corresponding to the cell where the mouse pointer is.

 TMyDBGrid = class(TDBGrid);

procedure TForm1.DBGrid1DblClick(Sender: TObject);
  ACol : Integer;
  Pt : TPoint;
  CellValue : String;
  //  First, get the mouse pointer coordinates
  Pt.X := Mouse.CursorPos.X;
  Pt.Y := Mouse.CursorPos.Y;
  //  Translate them into the coordinate system of the DBGrid
  Pt := DBGrid1.ScreenToClient(Pt);

  //  Use TDBGrids inbuilt functionality to identify the Column and
  //  row number. 
  ACol := DBGrid1.MouseCoord(Pt.X, Pt.Y).X -1;
  ARow := DBGrid1.MouseCoord(Pt.X, Pt.Y).Y;
  CellValue := DBGrid1.Columns[ACol].Field.AsString;

  Caption := Format('Col:%d Row:%d Cell Value:%s', [ACol, ARow, CellValue]);

As noted in a comment, you can use the grid's SelectedField property instead of the above, but personally I think the above is more instructive of how to work with a DBGrid. See the DBGrid's SelectedField, SelectedIndex and SelectedRows properties in the Online Help for more info.