RAD Studio |  |

Changing the Associated Dataset at Runtime
See Also
Collapse All

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:

Copy Code

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

Copy Code

    if (CustSource->DataSet == Customers)
      CustSource->DataSet = Orders;
    else
      CustSource->DataSet = Customers;

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

Copy Code

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

Copy Code

    void __fastcall TForm2::FormCreate(TObject *Sender)
    {
      DataSource1->DataSet = Form1->Table1;
    }

See Also

Enabling and Disabling the Data Source

Responding to Changes Mediated by the Data Source

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!