• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2115
  • Last Modified:

DBGrid and DblClick event

I would like to know which record a user selected when he doubleclicks on the DBGrid component.  I think it can be done by using the onDblClick event.  Then I take the current record (the first click selects the record in the grid, the second click triggers the event handler).

But that is not always correct !

Suppose I have a DBGrid component that is half filled with records.  A part of the window is empty.  When a user doubleclicks on the empty part the DblClick event will be triggered, but I shouldn't do anything, because he didn't click on a record in the grid.  But how can I know that ?  OnDblClick has no parameters ???  The current record isn't correct, because when you click on the empty part, the last record will be selected.

Thanks in advance,

Walter.
0
lecossois
Asked:
lecossois
1 Solution
 
333Commented:
Hi,
if you know coordinates, where mouse was pressed, then use MouseCoord function:

procedure TForm1.DBGrid1DblClick(Sender: TObject);
var
  Coord:TGridCoord;
begin
  Coord:=DBGrid1.MouseCoord(x,y); {where x and y are mouse coords}
end;

Now Coord.X is Column, and Coord.Y is Row in DBGrid.
I think now you can calculate record number

A.
0
 
viktornetCommented:
You can do like this.,..

MyXValue := Selection.Left;
MyYValue := Selection.Top;

Regards,
Viktor Ivanov
0
 
nileqCommented:
Hi!
DBGrid is a "data aware" - component, ie it's a window to the database. When a user clicks on a record in the grid the pointer in your table will point to that record in your database.
Just enter the code that you want executed on the OnDblClick and use the table or query component to find out what record he/she clicked on.

Procedure TForm1.DBgrid1OnDblClick(Sender : TObject);
Begin
       Showmessage('You clicked on: '+Table1.FieldByName('Name').AsString);
end;

Have a good day!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lecossoisAuthor Commented:
As I explained in my question, the solution nileq proposes is not always correct.  If the grid is not completely filled and the user DblClicks I should be able to find out that he clicked in the empty space.

The comment from 333 is very helpful, although I don't know how to determine that the mouse pointer is in the empty space of the grid.
0
 
333Commented:
Hi,
there is procedure MouseDown in TDBGrid . But there's no event like OnMouseDown.
You can override MouseDown procedure.
Here is an example:

{The Form (Form1), has Table (Table1) and DataSource (DataSource1).
There is NO DBGrid placed at design time. It will be created on run time}
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, DBGrids, StdCtrls, Db, DBTables;

type
  TMyGrid=class(TDBGrid)
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  end;

  TForm1 = class(TForm)
    Table1: TTable;
    DataSource1: TDataSource;
    procedure FormCreate(Sender: TObject);
  private
  public
    DBGrid:TMyGrid;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TMyGrid.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  Coord:TGridCoord;
begin
  inherited MouseDown(Button,Shift,X,Y);
  Coord:=MouseCoord(X,Y);
  {here you must check if cell is selected
  Coord.X is cell's column, Corrd.Y - row
  if you pressed mouse on empty area, then Coord.X and Coord.Y are (-1)}
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DBGrid:=TMyGrid.Create(Self);
  DBGrid.Parent:=Form1;
  DBGrid.DataSource:=DataSource1;
  {Define other DBGrid values like Top, Left, etc. here}
end;

end.
0
 
lecossoisAuthor Commented:
Ok, 333, you deserve the points.  By since you gave a comment I can't accept your answer.  Why don't you post an answer (the contents is no longer important), and I will accept it.

Thank you !
0
 
333Commented:

       if you know coordinates, where mouse was pressed, then use MouseCoord function:

       procedure TForm1.DBGrid1DblClick(Sender: TObject);
       var
         Coord:TGridCoord;
       begin
         Coord:=DBGrid1.MouseCoord(x,y); {where x and y are mouse coords}
       end;

       Now Coord.X is Column, and Coord.Y is Row in DBGrid.
       I think now you can calculate record number

       A.
0
 
N_R_GunneCommented:
There is another solution I worked out recently. I had problems to load a record on DblClick in place of single click. So I am using two datasets and datasources: one dataset as source for the grid and a TQuery, which loads the selected record for the DB-Controls.

procedure TFormMain.DatasetGridDblClick(Sender: TObject);
begin
  if not DatasetGrid.IsNull // if IsNull = true then the user may have doubleclicked an empty row
  then begin
    SelectRecord(DatasetGrid.FieldByName('ID').AsInteger);
    DatasetGrid.RefreshDisplay;
  end;
end;

..., where SelectRecord looks like follows:

procedure TFormMain.SelectRecord(const AId: Longint);
begin
  if QueryControls.State in dsEditModes
  then QueryControls.Post;

  FId := AId;  // FId: private field for remembering which record is loaded

  QueryControls.Close;
  QueryControls.ParamByName('ID').AsInteger := FId;
  QueryControls.Open;
end;

The QueryControls.SQL-Statement: 'SELECT * FROM table WHERE ID=:Id', where 'table' is the name of the table and ':Id' is the param.

Maybe this could be an alternative solution for your problem if it has not been solved till now.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now