好吧,某种问题来自我。我在这里上网和类似的问题,但没有找到这么简单(我认为)问题的正确答案。

我有一个DBGrid。我选择一行并使用链接到此行的其他数据执行一些操作。完成后,我的DBGrid被刷新并且选择的行重置为第一个。我想获取在刷新DBGrid数据之前选择的相同行。有什么建议吗?

有帮助吗?

解决方案

在刷新之前,将链接数据集的当前选择保存为书签,然后恢复书签。

其他提示

这个答案是作为梅森的一个小补充,而不是替代。我添加它只是因为出现了另一个答案,建议错误地使用数据集的RecNo属性。并非所有TDataSet后代都可靠地或完全实现RecNo。一些后代只返回一个常数值,例如0表示当前行的RecNo,并且在为其赋值时不执行任何操作。

procedure TMyForm.DoSomethingWithDataSet(ADataSet : TDataSet);
var
  Bookmark : TBookmark;
begin
  Bookmark := ADataSet.GetBookmark; // Save your place in ADataSet

  try
    Screen.Cursor := crSqlWait;  // Show the user that something is happening
    Update;  // update the form to make sure screen cursor updates
    ADataSet.DisableControls;
    // do something with ADataSet here  e.g.
    ADataSet.First;
    while not ADataSet.Eof do begin
      // do something with current row here, then
      ADataSet.Next;
    end;
  finally
    ADataSet.GotoBookmark(Bookmark);  // Return to where you were at outset
    ADataSet.FreeBookmark(Bookmark);
    ADataSet.EnableControls;
    Screen.Cursor := crDefault;  // Let the user see you're done
  end;
end;
RecKey:=DmFRM.ViewTBL.RecNo;
          with DmFRM.ViewTBL do
               begin
                  Active:=false;
                  Active:=True;
                  RecNo:=RecKey;
               end;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top