I want to link a TGrid to a TDataSource at runtime.

I want to link a TGrid to a TDataSource at runtime.

Can anyone point me to an up to date example of how to achieve this please?

Comments

  1. I have managed to get it linked, but the grid only displays the headers. Here's my code, any help appreciated.

    procedure TForm1.Button1Click(Sender: TObject);
    var
    BindSourceDB1: TBindSourceDB;
    BindingsList1: TBindingsList;
    ALinkGridToDataSourceBindSourceDB: TLinkGridToDataSource;
    grid1: TGrid;
    begin
    grid1 := TGrid.Create(Self);
    grid1.Parent := Form1;
    grid1.Align := TAlignLayout.Client;

    BindSourceDB1 := TBindSourceDB.Create(Self);
    BindSourceDB1.DataSet := UniQuery1;

    BindingsList1 := TBindingsList.Create(Self);
    BindingsList1.UseAppManager := True;

    ALinkGridToDataSourceBindSourceDB := TLinkGridToDataSource.Create(BindingsList1);
    ALinkGridToDataSourceBindSourceDB.DataSource := BindSourceDB1;
    ALinkGridToDataSourceBindSourceDB.GridControl := grid1;
    ALinkGridToDataSourceBindSourceDB.Category := 'Quick Bindings';
    end;

    ReplyDelete
  2. TLinkObservers.ControlChanged(LiveGrid); will update the data in a changed LiveBinding control. I also use this to change the datasource that a LiveGrid is linked to:
    LinkGridToDataSourceBindSourceDBData.DataSource := BindSourceDB;
    LinkGridToDataSourceBindSourceDBData.Active := True;

    ReplyDelete
  3. Thanks Eli that worked!
    I added the LinkGridToDataSourceBindSourceDBData.Active := True and also the TLinkObservers.ControlChanged(grid1);

    ReplyDelete

Post a Comment