如何在返回到前一个位置时避免TDbgrid滚动 [英] How to avoid TDbgrid scrolling when returning to a previous location
问题描述
在下面的代码中,我们对某些选定的行执行一些操作(而不是删除)。
In the code below, we do some operations (not deletions) on some selected rows.
但是,有时候,完成后,顶部选中的行已滚动它以网格的1/2方式显示。有没有办法避免这种滚动? (如果我的代码遍历下面选择的行是不正确的,因为一些无关的原因,我欢迎更正。)
However, sometimes, when finished, the top selected row has scrolled so that it is displayed 1/2 way down the grid. Is there a way to avoid this scrolling? (If my code to traverse the selected row below is improper for some unrelated reason, I welcome corrections.)
Function TForm.DoSomethingToSelectedRows;
var
KeyAtStart: Integer;
begin
Result := TRUE;
KeyAtStart := DataSet.FieldByName('Key').AsInteger;
DataSet.DisableControls;
DataSet.First;
try
while Result AND (NOT DataSet.EOF) do DataSet
begin
if DBGrid1.SelectedRows.CurrentRowSelected then
Result := ... do something ...
fMPODataTls.GetDS.Next;
end;
finally
DataSet.Locate('Key', KeyAtStart, []); // re-position where we started
DataSet.EnableControls;
end;
end;
推荐答案
循环访问数据集之前,可以记下网格显示的顶行和显示的总记录数。有了这些信息,在您重新定位记录后,您可以将记录移动到顶部或底部,然后再次移动,将记录定位到确切的行。
Before looping through the dataset, you can keep note of the top row that the grid is displaying and the number of total records that is displayed. With this information, after you relocate the record, you can position the record to the exact row by moving either to the top or to the bottom and then moving back again.
您可以通过 MoveBy
执行移动。不幸的是,行
和 RowCount
属性 TDBGrid
被保护,因为你必须使用所谓的受保护的黑客。
You can carry out the moving by MoveBy
s. Unfortunately the Row
and RowCount
properties of TDBGrid
is protected, for that you have to use what is widely known as 'protected hack'.
在这个的答案,连同检查书签,以便你不会记录错误。
There's code example in this answer, together with checking bookmarks, so that you don't end up on a wrong record.
这篇关于如何在返回到前一个位置时避免TDbgrid滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!