tcsh tcsh - 2 months ago 15
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);
begin
if dbmodule.comenziDataSet.Active then
begin
if not Assigned(dbgridCelulaForm) then
begin
dbgridCelulaForm := TdbgridCelulaForm.Create(Self);
dbgridCelulaForm.DBMemoCelula.DataSource := dbmodule.comenziSource;
end;

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


dbgridCelulaForm = name of the form containing the
TDBMemo

DBMemoCelula = name of the
TDBMemo

dbmodule.comenziDataSet = comenziDataSet is the name of the
DataSet
and dbmodule is the name of a
data module
(unit, like forms) - the
DataSet
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
DBGrid
it pops up a
form
(named dbgridCelulaForm) which contains a
TBMemo
(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
DBGrid
, 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
form
pops up.

Is it possible to use this code in
DBGrid
-
OnDblClick
event instead of the
OnCellClick
?
Meaning once i double click a row / cell the
form
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.

Thanks!

Answer

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.

type
 TMyDBGrid = class(TDBGrid);

procedure TForm1.DBGrid1DblClick(Sender: TObject);
var
  ARow,
  ACol : Integer;
  Pt : TPoint;
  CellValue : String;
begin
  //  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]);
end;

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.