Select row after refreshing DBGrid
Asked Answered
A

3

5

Well, some kind of n00b question from me. I've surfed the net and similar questions here but haven't found any right answers for such simple (as I thought) problem.

I have a DBGrid. I select one row and make some actions with another data linked to this row. After I finished, my DBGrid being refreshed and selected row resets to first. I want to get the same row selected that was selected before refreshing DBGrid data. Any suggestions?

Aleron answered 25/10, 2009 at 11:8 Comment(0)
S
4

Before refreshing, save the linked dataset's current selection as a bookmark, then restore the bookmark afterwards.

Saccharoid answered 25/10, 2009 at 11:30 Comment(0)
T
4

This answer is intended as a minor supplement to Mason's, not an alternative. I've added it only because another answer has appeared, suggesting, incorrectly imo, the use of the dataset's RecNo property. Not all TDataSet descendants implement RecNo reliably or at all. Some descendants just return a constant value e.g. 0 for the current rows's RecNo and do nothing when you assign a value to it.

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;
Tank answered 27/7, 2014 at 9:2 Comment(3)
Boomark works like RecNo ? or not ? , for example : if I get the bookmark , then delete some records before of selected record and next I go to bookmark , now the selected record is same as the selected before delete ? , in other words : Bookmark store just RecNo or Position of record or it stores another info about selected record ?Paddie
I think it depends on how bookmarks are implemented in the particular descendanf or TDataset, but I'd be surprised if any one the common ones use merely a record number. From the few I've looked into, they typically use a buffer which contains implementation-specific data. If in doubt about whether a given type of dataset implements bookmarks, call the BookmarkValid function (see OLH).Tank
thanks , I tried GetBookmark and GotoBookmark for a deleted record : GetBookmark -> Delete Selected ( Bookmarked ) Record -> GotoBookmark , and I got an error : "Record not found" , I have a table that should refresh each second and I want to keep users scrolling , in this situation I think I should use RecNo , I tried RecNo and works finePaddie
T
-1
RecKey:=DmFRM.ViewTBL.RecNo;
          with DmFRM.ViewTBL do
               begin
                  Active:=false;
                  Active:=True;
                  RecNo:=RecKey;
               end;
Trilbee answered 27/7, 2014 at 0:23 Comment(3)
I see that this already has a -1, so I will not add mine. The reason for the -1 is probably that not all TDataSet descendants implement RecNo usefully or in some cases, at all. So, it is not useful as a general answer to the Op's question, I'm afraid,Tank
@Tank : " GetBookmark relies on a protected method to obtain the bookmark value. TDataSet descendants implement this method to provide their own type of bookmark support. Unidirectional datasets do not support bookmarks, and so do not return a meaningful value."Paddie
@Mahmood_M: I'm not sure why you're quoting that to me. Unidirectional datasets may not support bookmarks, but it's for the same reason that they cannot be connected directly to a TDBGrid, namely that the grid relies on the dataset being navigable in both directions. So, to use a unidirectional dataset to TDBGrid, you have to connect the grid to a TClientDataset and load that from the unidirectional one.Tank

© 2022 - 2024 — McMap. All rights reserved.