DBGrid mousedown and record number lazarus / 16-Databases ---------------------------------------------------------------------- xinyiman · 2018-09-16 21:17 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 ---------------------------------------------------------------------- howardpc · 2018-09-16 22:26 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 · 2018-09-17 05:53 Quote from: xinyiman on September 16, 2018, 11:17:08 pmI'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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.MemDataset1AfterScroll(DataSet: TDataSet);begin     Self.Caption := Self.MemDataset1.FieldByName('field1').AsString;end; procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);var   col, row: integer;begin     // Self.DBGrid1.MouseToCell(x,y,col,row);     // Self.Caption := IntToStr(row);end; ---------------------------------------------------------------------- xinyiman · 2018-09-17 06:04 Quote from: howardpc on September 17, 2018, 12:26:36 amYour 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. ---------------------------------------------------------------------- xinyiman · 2018-09-17 06:05 Quote from: valdir.marcos on September 17, 2018, 07:53:32 amQuote from: xinyiman on September 16, 2018, 11:17:08 pmI'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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.MemDataset1AfterScroll(DataSet: TDataSet);begin     Self.Caption := Self.MemDataset1.FieldByName('field1').AsString;end; procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);var   col, row: integer;begin     // Self.DBGrid1.MouseToCell(x,y,col,row);     // Self.Caption := IntToStr(row);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. ---------------------------------------------------------------------- paweld · 2018-09-17 07:07 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);var  col, row: integer;begin  Self.DBGrid1.MouseToCell(x,y,col,row);  Self.Caption:=IntToStr(DBGrid1.DataSource.DataSet.RecNo- TSTringGrid(DBGrid1).Row+row);end; ---------------------------------------------------------------------- howardpc · 2018-09-17 08:05 The TStringGrid cast does not compile for me. Try this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  THackGrid = class(TCustomDBGrid)  public    property Row;  end; procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);var   col, row: integer;   hackGrid: THackGrid absolute Sender;begin     DBGrid1.MouseToCell(X, Y, col, row);     case row of       0: Caption := 'mouse not on a dataset record';       else Caption:=IntToStr(DBGrid1.DataSource.DataSet.RecNo - hackGrid.Row + row);     end;end; ---------------------------------------------------------------------- paweld · 2018-09-17 08:56 Add Grid unit to uses section ---------------------------------------------------------------------- xinyiman · 2018-09-17 20:16 Quote from: howardpc on September 17, 2018, 10:05:57 amThe TStringGrid cast does not compile for me. Try this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  THackGrid = class(TCustomDBGrid)  public    property Row;  end; procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);var   col, row: integer;   hackGrid: THackGrid absolute Sender;begin     DBGrid1.MouseToCell(X, Y, col, row);     case row of       0: Caption := 'mouse not on a dataset record';       else Caption:=IntToStr(DBGrid1.DataSource.DataSet.RecNo - hackGrid.Row + row);     end;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? ---------------------------------------------------------------------- Soner · 2018-09-17 21:49 Quote from: xinyiman on September 17, 2018, 10:16:17 pmOk, 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  THackGrid = class(TCustomDBGrid)  public      function RecNoFromVisibleRow(Value: Integer): Integer;  end; function THackGrid.RecNoFromVisibleRow(Value: Integer): Integer;begin  Result := Datalink.DataSet.RecNo - Row + TopRow + Value;  if dgTitles in Options then Dec(Result);end; procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);var   arow: integer;begin     DBGrid1.MouseToCell(X, Y, col, arow);     if arow<1 then //when you hide header row then arow<0       0: Caption := 'mouse not on a dataset record';     else Caption:=IntToStr(THackGrid(DBGrid1).RecNoFr omVisibleRow(arow));end;  Edit: TDbGrid is virtual, this means TDbGrid.RowCount=VisibleRowCount. ---------------------------------------------------------------------- xinyiman · 2018-09-20 07:01 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? ---------------------------------------------------------------------- xinyiman · 2018-09-21 09:28 no suggestion?