I'm trying to get on a mousedown the number of the record on which I want to position myself.Setting MemDataset1AfterScroll event will do the trick:
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.
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'm trying to get on a mousedown the number of the record on which I want to position myself.Setting MemDataset1AfterScroll event will do the trick:
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.
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;
The TStringGrid cast does not compile for me.
Try this
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?