Changing the Associated Dataset at Runtime

From RAD Studio
Jump to: navigation, search

Go Up to Associating a Data Control with a Dataset


In Associating a Data Control with a Dataset, the datasource was associated with its dataset by setting the DataSet property at design time. At runtime, you can switch the dataset for a data source component as needed. For example, the following code swaps the dataset for the CustSource data source component between the dataset components named Customers and Orders:

 with CustSource do begin
   if (DataSet = Customers) then
     DataSet := Orders
   else
     DataSet := Customers;
 end;

You can also set the DataSet property to a dataset on another form to synchronize the data controls on two forms. For example:

 procedure TForm2.FormCreate (Sender : TObject);
 begin
   DataSource1.Dataset := Form1.Table1;
 end;

See Also