Recent

Author Topic: DBGrid mousedown and record number  (Read 7663 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
DBGrid mousedown and record number
« on: September 16, 2018, 11:17:08 pm »
Hi guys, I'm trying to get on a mousedown the number of the record on which I want to position myself. If you run the test program that I have attached and you put on the line with field1 = 60 I expect in the Self.Caption to see 60 instead I see other numbers. What escapes me?

Thank you
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: DBGrid mousedown and record number
« Reply #1 on: September 17, 2018, 12:26:36 am »
Your method returns the row number of the GUI grid, not the record number of the currently displayed record.
Your form has sufficient height for the DBGrid to display 10 grid rows, of which the first row is metadata (field names).
The position of the grid scroll thumb controls which dataset records are scrolled to display in the grid. The grid row number bears no relation to the record number of the data record that happens to have populated that grid row.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: DBGrid mousedown and record number
« Reply #2 on: September 17, 2018, 07:53:32 am »
I'm trying to get on a mousedown the number of the record on which I want to position myself.
If you run the test program that I have attached and you put on the line with field1 = 60 I expect in the Self.Caption to see 60 instead I see other numbers.
Setting MemDataset1AfterScroll event will do the trick:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MemDataset1AfterScroll(DataSet: TDataSet);
  2. begin
  3.      Self.Caption := Self.MemDataset1.FieldByName('field1').AsString;
  4. end;
  5.  
  6. procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  7.   Shift: TShiftState; X, Y: Integer);
  8. var
  9.    col, row: integer;
  10. begin
  11.      // Self.DBGrid1.MouseToCell(x,y,col,row);
  12.      // Self.Caption := IntToStr(row);
  13. end;
« Last Edit: September 17, 2018, 07:59:28 am by valdir.marcos »

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: DBGrid mousedown and record number
« Reply #3 on: September 17, 2018, 08:04:49 am »
Your method returns the row number of the GUI grid, not the record number of the currently displayed record.
Your form has sufficient height for the DBGrid to display 10 grid rows, of which the first row is metadata (field names).
The position of the grid scroll thumb controls which dataset records are scrolled to display in the grid. The grid row number bears no relation to the record number of the data record that happens to have populated that grid row.

I had noticed it as I wrote the example. But it was to make people understand what I was trying to achieve.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: DBGrid mousedown and record number
« Reply #4 on: September 17, 2018, 08:05:53 am »
I'm trying to get on a mousedown the number of the record on which I want to position myself.
If you run the test program that I have attached and you put on the line with field1 = 60 I expect in the Self.Caption to see 60 instead I see other numbers.
Setting MemDataset1AfterScroll event will do the trick:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MemDataset1AfterScroll(DataSet: TDataSet);
  2. begin
  3.      Self.Caption := Self.MemDataset1.FieldByName('field1').AsString;
  4. end;
  5.  
  6. procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  7.   Shift: TShiftState; X, Y: Integer);
  8. var
  9.    col, row: integer;
  10. begin
  11.      // Self.DBGrid1.MouseToCell(x,y,col,row);
  12.      // Self.Caption := IntToStr(row);
  13. end;

Thanks for the tip, but it's not good. Because I have to get it if I pressed the right mouse button. Hence the need to put it in mousedown.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: DBGrid mousedown and record number
« Reply #5 on: September 17, 2018, 09:07:54 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. var
  4.   col, row: integer;
  5. begin
  6.   Self.DBGrid1.MouseToCell(x,y,col,row);
  7.   Self.Caption:=IntToStr(DBGrid1.DataSource.DataSet.RecNo-TSTringGrid(DBGrid1).Row+row);
  8. end;
Best regards / Pozdrawiam
paweld

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: DBGrid mousedown and record number
« Reply #6 on: September 17, 2018, 10:05:57 am »
The TStringGrid cast does not compile for me.
Try this
Code: Pascal  [Select][+][-]
  1. type
  2.   THackGrid = class(TCustomDBGrid)
  3.   public
  4.     property Row;
  5.   end;
  6.  
  7. procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  8.   Shift: TShiftState; X, Y: Integer);
  9. var
  10.    col, row: integer;
  11.    hackGrid: THackGrid absolute Sender;
  12. begin
  13.      DBGrid1.MouseToCell(X, Y, col, row);
  14.      case row of
  15.        0: Caption := 'mouse not on a dataset record';
  16.        else Caption:=IntToStr(DBGrid1.DataSource.DataSet.RecNo - hackGrid.Row + row);
  17.      end;
  18. end;

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: DBGrid mousedown and record number
« Reply #7 on: September 17, 2018, 10:56:44 am »
Add Grid unit to uses section
Best regards / Pozdrawiam
paweld

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: DBGrid mousedown and record number
« Reply #8 on: September 17, 2018, 10:16:17 pm »
The TStringGrid cast does not compile for me.
Try this
Code: Pascal  [Select][+][-]
  1. type
  2.   THackGrid = class(TCustomDBGrid)
  3.   public
  4.     property Row;
  5.   end;
  6.  
  7. procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  8.   Shift: TShiftState; X, Y: Integer);
  9. var
  10.    col, row: integer;
  11.    hackGrid: THackGrid absolute Sender;
  12. begin
  13.      DBGrid1.MouseToCell(X, Y, col, row);
  14.      case row of
  15.        0: Caption := 'mouse not on a dataset record';
  16.        else Caption:=IntToStr(DBGrid1.DataSource.DataSet.RecNo - hackGrid.Row + row);
  17.      end;
  18. end;

Ok, thanks, it works great, both your version and that of paweld. One question, is it to get the record number corresponding to the correct toprow as I do?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Soner

  • Sr. Member
  • ****
  • Posts: 305
Re: DBGrid mousedown and record number
« Reply #9 on: September 17, 2018, 11:49:49 pm »
Ok, thanks, it works great, both your version and that of paweld. One question, is it to get the record number corresponding to the correct toprow as I do?


Code: Pascal  [Select][+][-]
  1. type
  2.   THackGrid = class(TCustomDBGrid)
  3.   public
  4.       function RecNoFromVisibleRow(Value: Integer): Integer;
  5.   end;
  6.  
  7. function THackGrid.RecNoFromVisibleRow(Value: Integer): Integer;
  8. begin
  9.   Result := Datalink.DataSet.RecNo - Row + TopRow + Value;
  10.   if dgTitles in Options then Dec(Result);
  11. end;
  12.  
  13. procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  14.   Shift: TShiftState; X, Y: Integer);
  15. var
  16.    arow: integer;
  17. begin
  18.      DBGrid1.MouseToCell(X, Y, col, arow);
  19.      if arow<1 then //when you hide header row then arow<0
  20.        0: Caption := 'mouse not on a dataset record';
  21.      else Caption:=IntToStr(THackGrid(DBGrid1).RecNoFromVisibleRow(arow));
  22. end;
  23.  
  24.  
Edit:
TDbGrid is virtual, this means TDbGrid.RowCount=VisibleRowCount.
« Last Edit: September 17, 2018, 11:53:06 pm by Soner »

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: DBGrid mousedown and record number
« Reply #10 on: September 20, 2018, 09:01:40 am »
I have updated the example and if you try to run it now you will see that I try to open a menu on the right mouse button (mousedown). It works well because it selects the right record but moves me to view the records. The toprow property is used on the stringgrid. Here how do I do it?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: DBGrid mousedown and record number
« Reply #11 on: September 21, 2018, 11:28:34 am »
no suggestion?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018