t1f
t1f

Reputation: 3181

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!

Upvotes: 0

Views: 3625

Answers (1)

MartynA
MartynA

Reputation: 30715

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;

Note that I've used the Caption property of the form to display the grid cell info just as a quick n dirty way of showing the information somewhere. Of course you could equally well display it on another area of the form or somewhere on a different form entirely. The above code will work equally well in the grid's OnCellClick event, btw.

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, because it shows how to get the cell's Column and Row coordinates. See the DBGrid's SelectedField, SelectedIndex and SelectedRows properties in the Online Help for more info on useful properties of the TDBGrid.

Update You asked in a comment for an example of showing the information on another form. Let's suppose this form is called OtherForm, is in a unit OtherFormu.Pas and is created before the DBGrid1DblClick evenbt is called. You need to use this unit in the Useslist of the unit which contains the DBGrid. Let's suppose this other form contains a TMemo control called Memo1. Then, you could write your DBGrid1DblClick hanndler like this:

procedure TForm1.DBGrid1DblClick(Sender: TObject);
[as above]
begin
  [ as above ]
  CellValue := DBGrid1.Columns[ACol].Field.AsString;

  OtherForm.Memo1.Lines.Add(Format('Col:%d Row:%d Cell Value:%s', [ACol, ARow, CellValue]));
end;

Upvotes: 3

Related Questions