简体   繁体   中英

Connect database table to StringGrid with LiveBindings via code

I want to use LiveBindings to connect a database table to a StringGrid, but I don't want to use the LiveBindings Designer, I want to do it manually via code. The documentation is in my opinion nearly not existing, which makes it more complicated than it should be.

I created a FMX application with my Delphi 10.3 and this is the code I wrote to do all I need:

procedure TForm_LiveBindings.CornerButton_Click(Sender: TObject); 
var
  aLinkTableToDataSource: TLinkGridToDataSource;
  aConnection: TADOConnection;
  aQuery: TADOQuery;
  aBindSource: TBindSourceDB;
begin
  aConnection:= TADOConnection.Create(self);
  aQuery:= TADOQuery.Create(self);
  aBindSource:= TBindSourceDB.Create(self);
  aLinkTableToDataSource:= TLinkGridToDataSource.Create(self);

  // Connection is set up here

  aQuery.Connection := aConnection;
  aQuery.SQL.Text := 'SELECT * FROM TestTable';
  aQuery.Active := True;

  aBindSource.DataSource.DataSet := aQuery;
  aBindSource.DataSource.AutoEdit := True;
  aBindSource.DataSource.Enabled := True;

  aLinkTableToDataSource.DataSource := aBindSource;
  aLinkTableToDataSource.GridControl := StringGrid1;

end;

The result: my StringGrid shows the table headers, but the rows stay empty. Which means the connection between the table and the string grid is existing, the columns have the correct header, but the content is not shown. So where did I go wrong?

Another question: is the StringGrid a good choice for displaying my database table or are there better solutions?

Thank a lot for your answers!

The example below shows all the code necessary to set up and populate a TStringGrid and a TGrid using Live Bindings. It uses a TClientDataSet as the dataset so that it is completely self- contained.

A bit of experimenting should satisfy you that setting up Live Bindings in code is actually quite simple, but sensitive to the order of steps. Far more so than using VCL and traditional db-aware controls, Live Bindings seems require connecting up exactly the right things in the right way for it to work correctly. Note that unlike your code, my code does not touch the BindSorce's Datasource property, because it just isn't necessary.

type
  TForm2 = class(TForm)
    ClientDataSet1: TClientDataSet;
    Grid1: TGrid;
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
  private
  public
  end;
[...]

procedure TForm2.FormCreate(Sender: TObject);
var
  AField : TField;
  BindSourceDB1 : TBindSourceDB;
  LinkGridToDataSourceBindSourceDB1 : TLinkGridToDataSource;
  LinkGridToDataSourceBindSourceDB2 : TLinkGridToDataSource;
begin
  AField := TIntegerField.Create(Self);
  AField.FieldName := 'ID';
  AField.FieldKind := fkData;
  AField.DataSet := ClientDataSet1;

  AField := TStringField.Create(Self);
  AField.FieldName := 'Name';
  AField.Size := 20;
  AField.FieldKind := fkData;
  AField.DataSet := ClientDataSet1;

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

  LinkGridToDataSourceBindSourceDB1 := TLinkGridToDataSource.Create(Self);
  LinkGridToDataSourceBindSourceDB1.DataSource := BindSourceDB1;
  LinkGridToDataSourceBindSourceDB1.GridControl := Grid1;

  LinkGridToDataSourceBindSourceDB2 := TLinkGridToDataSource.Create(Self);
  LinkGridToDataSourceBindSourceDB2.DataSource := BindSourceDB1;
  LinkGridToDataSourceBindSourceDB2.GridControl := StringGrid1;

  ClientDataSet1.IndexFieldNames := 'ID';
  ClientDataSet1.CreateDataSet;
  ClientDataSet1.InsertRecord([1, 'AName']);
  ClientDataSet1.InsertRecord([2, 'AnotherName']);
  ClientDataSet1.InsertRecord([3, 'ThirdName']);
  ClientDataSet1.InsertRecord([4, 'FourthName']);
  ClientDataSet1.InsertRecord([5, 'FifthName']);
  ClientDataSet1.First;

end;

Answering late, but maybe it helps someone. LinkGrid must be Activate by

aLinkTableToDataSource.Active:= True;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM