[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

DBGrid and DblClick event

Posted on 1998-10-06
8
Medium Priority
?
2,069 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 
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 400 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

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!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

656 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