开发者

How can I tell when a Delphi TDBGrid has finished populating from a database?

开发者 https://www.devze.com 2023-01-19 06:39 出处:网络
I have a database populating a TDBGrid in Delphi 开发者_JAVA技巧2007 Pro.When the grid finishes populating, I want to automatically fill a list box based on data processed from the grid.I can do this

I have a database populating a TDBGrid in Delphi 开发者_JAVA技巧2007 Pro. When the grid finishes populating, I want to automatically fill a list box based on data processed from the grid. I can do this manually by watching and waiting for the grid to completely fill with the dataset and then calling my next procedure. Is there an event that would allow calling the next procedure when the grid finishes populating automatically? Thanks.


If you're using a TDataSet descendant, you can use its AfterOpen event:

"AfterOpen is called after the dataset establishes access to its data and the dataset is put into dsBrowse state."


edit (code sample for comments for Duilio's answer): In the below, 'CDS' is a 'TClientDataSet'. A 'TDBGrid' is also attached to the data set by means of a 'TDataSource', but the grid's functionality is not in any way effected by the code below, or the ListBox's functionality with the grid's for that matter..

procedure TForm1.CDSAfterOpen(DataSet: TDataSet);
var
  sl: TStringList;
begin
  sl := TStringList.Create;
  try
    sl.Sorted := True;
    sl.Duplicates := dupIgnore;

    DataSet.DisableControls;
    try
      DataSet.First;
      while not DataSet.Eof do begin
        sl.Add(DataSet.Fields[1].AsString);
        DataSet.Next;
      end;
      DataSet.First;
    finally
      DataSet.EnableControls;
    end;

    ListBox1.Items.Assign(sl);
  finally
    sl.Free;
  end;
end;


I think you could execute:

TDataSet.Open;
TDataSet.FetchAll;
{At this point DBGrid should be populated}

This will get all the data from your table. When done, your DBGrid should be populated.

0

精彩评论

暂无评论...
验证码 换一张
取 消