Solved

DBGrid and DblClick event

Posted on 1998-10-06
8
1,803 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

14 Experts available now in Live!

Get 1:1 Help Now