Solved

DBGrid and DblClick event

Posted on 1998-10-06
8
1,847 Views
Last Modified: 2010-04-06
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
Comment
Question by:lecossois
8 Comments
 
LVL 2

Expert Comment

by:333
ID: 1342006
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1342007
You can do like this.,..

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

Regards,
Viktor Ivanov
0
 

Expert Comment

by:nileq
ID: 1342008
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
 

Author Comment

by:lecossois
ID: 1342009
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:333
ID: 1342010
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
 

Author Comment

by:lecossois
ID: 1342011
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
 
LVL 2

Accepted Solution

by:
333 earned 100 total points
ID: 1342012

       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
 

Expert Comment

by:N_R_Gunne
ID: 8804427
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert Jpg /PNG To GIF 5 120
Intraweb download file link ? 1 110
creating threads in delphi 1 84
RESTRequest Parameter 4 15
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now