1
votes

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.

2
What TDataSet descendant is yor TDBGrid linked to? First, remember the DBGrid is just a "window" to display the DataSet data, so maybe you're looking for an event over the DataSet (look @Sertac Akyuz answer). On the other hand, you also have TDBListBox component which you can associate with your DataSource and it is automatically "filled", like the TDBGrid.jachguate

2 Answers

3
votes

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;
0
votes

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.