Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 2 of 2 total
Thread TDataSet.Locate
Fri, Jun 21 2013 2:58 AMPermanent Link

Christian Kaufmann

Hi,

after posting an action to my server, I want to reload a TDataSet and
have the same record selected in my TGrid.

After reload, the number and order of rows may be different. It may
even happen, that the last active record isn't in the TDataSet
anymore.

All my records have a unique 'id' column. In Delphi I would do:

oldId := myData.Columns['id'].AsInteger;
// reload dataset
myData.Locate('id', oldId);
if myData.Eof
 then myData.First;

What is simplest way to realize that?

I noticed, that every TDataRow has an ID, but this is is used for
default sort and in comments I read, that there shouldn't be any
holes. So I cannot work with this one.

cu Christian
Fri, Jun 21 2013 11:28 AMPermanent Link

Uli Becker

Christian,
> What is simplest way to realize that?

There is a sample in the help files that might help you:

Searching for a Row
--------------------------------------------------------------------------------
The TDataSet InitFind and Find methods allow you to search the rows in
the dataset for a particular set of column values. The first step to
executing a search is to call the InitFind method, which puts the
dataset in the "Find" state, which is represented by the TDataSet State
property. Once the dataset is in the "Find" state, you can assign values
to the columns in the dataset and then call the Find method to execute
the actual search. If there is a sort active on the dataset, then it
will be used for satisfying the Find operation if the modified columns
and the CaseInsensitive parameter to the Find method match the active
sort. For example, the following example sorts the Products dataset by
the ProductID column and then executes a case-insensitive Find operation
on the ProductID column for the 'PEN-BP-12PK' product ID:

begin
   with Products do
      begin
      Columns['ProductID'].SortDirection:=sdAscending;
      SortCaseInsensitive:=True;
      Sort;
      InitFind;
      Columns['ProductID'].AsString:='PEN-BP-12PK';
      if Find(False,True) then
         Result:=True
      else
         Result:=False;
      end;
end;

Uli
Image