SteveJG
SteveJG

Reputation: 256

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

I have a database populating a TDBGrid in Delphi 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.

Upvotes: 1

Views: 2391

Answers (2)

Sertac Akyuz
Sertac Akyuz

Reputation: 54792

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;

Upvotes: 3

Duilio Juan Isola
Duilio Juan Isola

Reputation: 237

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.

Upvotes: 0

Related Questions