Avoiding TDbgrid Scrolling When Returning to Previous Location

In the code below, we perform some operations (not deletions) on some selected lines.

However, sometimes, when finished, the top selected row scrolls so that it appears 1/2 way down the grid. Is there any way to avoid this scrolling? (If my code for passing the selected line below does not work for some 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;
+4
source share
3 answers

, , , . , , .

MoveBy s. , Row RowCount TDBGrid , , " ".

this, , .

+1

, , Locate, , ( ) , .
, , .

+1

TClientDataset, , .

DBGrid1, ClientDataSet1, 5 DBGrid ( ClientDataSet) , , d , , .

Function TForm.DeleteFirstRow;
var
  myCDS: TClientDataSet;
begin
  myCDS := myCDS.Create(nil);
  try
    myCDS.CloneCursor(ClientDataSet1, False);
    myCDS.First;
    myCDS.Delete;
  finally
    myCDS.Free;
  end;

If I scroll down to line 500 or so, outside the visibility of the first line and execute it again, the selected line would not move at all in the grid.

0
source

All Articles