What row of the dataset is displayed in the topmost row of the DBGrid when the first row of the grid is not selected by RowCount> VisibleRowCount?

Is it possible to get the row number that is displayed from the base dataset in the top row of the DBGrid, and this top row is the currently selected row when the number of records in the base dataset is equal to the number of rows displayed in the DBGrid and the DBGrid scrolls.

Here is my problem. From the drag / snap event handler bound to the DBGrid, I can determine which of the visible rows of the DBGrid the redirect event is associated with using MyGrid.MouseCoord (X, Y) .Y. If the base dataset contains less or the same number of records than the number of rows displayed in the DBGrid, this value is also the row number of the corresponding record in the base dataset.

When the underlying dataset contains more records than the number of rows visible in the DBGrid, MyGrid.MouseCoord (X, Y) .Y and TDataSet (MyGrid.DataSource.DataSet) .RecNo is the same only when the first row the dataset appears in the first row of the grid.

Is there a way to identify the record number in the base dataset (or offset) of the topmost displayed record in DBGrid without selecting this DBGrid row? I know that if I actually select the topmost row of the DBGrid, then I can use TDataSet (MyGrid.DataSource.DataSet) .RecNo to get the current record number of the underlying dataset. However, from the events DBGrid.OnDragOver or DBGrid.OnDragDrop, I only have a link to DBGrid and mouse coordinates (from which I can determine which grid line was the target of the fall).

For example, if I can determine that DBGrid displays the third record in the base dataset at the very top line of the grid, my problem is resolved. Similarly, if I can read the underlying TField of a specific line (say, the very top line) without selecting that line, I have what I need. However, I see no way to do this.

Any suggestions would be greatly appreciated.

Edit: I previously posted a drag and drop blog on DBGrid. With this new information, I can solve a previously known problem. I will be updating this blog this week and will add a link to this blog here as soon as I do.

There is an additional problem. When the number of visible lines is less than the number of base records, we also need to calculate the drop when it occurs after the last visible line. When dropped after the last visible line, MouseCoord (x, y) .Y returns -1.

Here is a modification of the Uwe code that performs this task:

function TDBGridHelper.RecNoFromVisibleRow(Value: Integer): Integer; begin if Value = -1 then begin Result := DataSource.DataSet.RecNo - Row + TopRow + VisibleRowCount end else begin Result := DataSource.DataSet.RecNo - Row + TopRow + Value; if dgTitles in Options then Dec(Result); end; end; 

Edit: As I mentioned in the original question, I was interested in this answer to fix the behavior in my code that implements drag and drop in DBGRid. With this answer, I updated the drag and drop behavior and wrote about this update on my blog. You can find this discussion, including links to the original blog post, at the following URL: Drag and Drop in DBGrid Revisited

+6
source share
1 answer

As long as there is only an offset between the DataSet.RecNo and the visible number of rows in the grid, you can get the necessary information from the protected members of Row and TopRow, which can be accessed using the class helper. Something like that:

 function TDbGridHelper.RecNoFromVisibleRow(Value: Integer): Integer; begin Result := DataSource.DataSet.RecNo - Row + TopRow + Value; if dgTitles in Options then Dec(Result); end; 
+6
source

Source: https://habr.com/ru/post/950967/


All Articles