Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Help binding a TFDMemTable to a TStringGrid at run time


This question is answered.


Permlink Replies: 10 - Last Post: Jan 21, 2016 1:56 AM Last Post By: Haley Winters
Haley Winters

Posts: 13
Registered: 3/11/13
Help binding a TFDMemTable to a TStringGrid at run time  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 12, 2016 11:22 PM
Hi All,

This might sound like a stupid question but it might be a little complex because I can't find a solution in Google Land.

I want to bind a StringGrid to a MemTable at run time and I have tried about five different methods but none have succeeded for me.

I am essentially looking at small databases - some are simply comma separated tables. I want to be able to select and load a file into a MemTable that is bound to a StringGrid. Because I am selecting files while the program is running I need to create the MemTable and the StringGrid live which I can do. I can't bind the StringGrid to the MemTable however - I can do this in visually in the IDE but I can't seem to implement it in code. There is an object called LiveBindings but I can't seem to get it to work.

I can create the grid and the MemTable live and I have designed code so that I can find whichever I want based on the TabControl that they are in.

What I want is a working code example of MemTable and StringGrid live binding.

While I am typing - maybe also an example of loading a CSV into the MemTable as well?

Best wishes for the new year,

Haley
Dmitry Arefiev

Posts: 1,406
Registered: 12/7/03
Re: Help binding a TFDMemTable to a TStringGrid at run time  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 13, 2016 8:32 PM   in response to: Haley Winters in response to: Haley Winters
What I want is a working code example of MemTable and StringGrid live binding.

http://stackoverflow.com/questions/18962678/delphi-livebinding-tclientdataset-tstringgrid-runtime

While I am typing - maybe also an example of loading a CSV into the MemTable as well?

See Object Pascal\Database\FireDAC\Samples\Comp Layer\TFDLocalSQL\MegaFMX
demo, TfrmMain.FDLocalSQL1GetDataSet method.

--
With best regards,
Dmitry
Haley Winters

Posts: 13
Registered: 3/11/13
Re: Help binding a TFDMemTable to a TStringGrid at run time  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 14, 2016 5:44 PM   in response to: Dmitry Arefiev in response to: Dmitry Arefiev
Hi Dmitry,

Thanks for your help - I already found the example but tried it again; here is my code :

TabSheet:= TTabSheet.Create(PageControl);
TabSheet.PageControl:= PageControl;

MemTable:= TFDMemTable.Create(TabSheet);

NewGrid := TStringGrid.Create(TabSheet);
NewGrid.Parent:= TabSheet;

BindSource:= TBindSourceDB(TabSheet); // 1

BindingsList:= TBindingsList.Create(TabSheet); // 2

LinkGridToDataSource:= TLinkGridToDataSource.Create(BindingsList); // 3

LinkGridToDataSource.DataSource:= BindSource; // 4

LinkGridToDataSource.GridControl:= NewGrid; // 5

BindSource.DataSource.Dataset:= MemTable; // 6

I first got an error at // 6 where TDataSource is incompatable with TMemTable so I used BindSource.DataSource.DataSet

Once I tried that I got an error at // 5 where I get a Invalid class typecast error. I really want to use a TStringGrid because of it's flexibility.

Any ideas?

Best regards,

Haley
Dmitry Arefiev

Posts: 1,406
Registered: 12/7/03
Re: Help binding a TFDMemTable to a TStringGrid at run time  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 14, 2016 8:26 PM   in response to: Haley Winters in response to: Haley Winters
BindSource:= TBindSourceDB(TabSheet); // 1

BindSource:= TBindSourceDB.Create(TabSheet); // 1

--
With best regards,
Dmitry
Haley Winters

Posts: 13
Registered: 3/11/13
Re: Help binding a TFDMemTable to a TStringGrid at run time  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 15, 2016 11:52 AM   in response to: Dmitry Arefiev in response to: Dmitry Arefiev
Hi Dmitry,

I just looked and found your solution - I'll load some data and test this today and get back to you!

Many thanks,

Haley
Haley Winters

Posts: 13
Registered: 3/11/13
Re: Help binding a TFDMemTable to a TStringGrid at run time  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 16, 2016 4:16 AM   in response to: Haley Winters in response to: Haley Winters
Hi Dmitry,

I have tried to load some data into the MemTable but it doesn't flow to the StringGrid.

I have a point where I make sure that the MemTable does actually have something in it.

The StringGrid does display but with only one blank title and one blank cell - the code is as follows :

TabSheet:= TTabSheet.Create(PageControl);
TabSheet.Caption:= ExtractFileName(FileName);
TabSheet.PageControl:= PageControl;

MemTable:= TFDMemTable.Create(TabSheet);

NewGrid := TStringGrid.Create(TabSheet);
NewGrid.Visible:= False;
NewGrid.Parent:= TabSheet;
NewGrid.Name := 'StringGrid' + IntToStr(PageControl.PageCount);

BindSource:= TBindSourceDB.Create(TabSheet); // 1
BindingsList:= TBindingsList.Create(TabSheet); // 2
LinkGridToDataSource:= TLinkGridToDataSource.Create(BindingsList); // 3
LinkGridToDataSource.DataSource:= BindSource; // 4
LinkGridToDataSource.GridControl:= NewGrid; // 5
BindSource.DataSource.DataSet:= MemTable; // 6

with MemTable.FieldDefs do begin
with AddFieldDef do begin
Name := 'f1';
DataType := ftInteger;
end;
with AddFieldDef do begin
Name := 'f2';
DataType := ftString;
Size := 50;
end;
end;

with MemTable do begin
Open;
Append;
Fields[0].AsInteger := 1000;
Fields[1].AsString := 'Record 1';
Post;
//end;

//with MemTable do begin
//Open;

Append;
Fields[0].AsInteger := 2000;
Fields[1].AsString := 'Record 2';
Post;
end;

ShowMessage(IntToStr(MemTable.RecordCount)); // Shows 2 records

Any further ideas?

Thanks for your help,

Haley
Dmitry Arefiev

Posts: 1,406
Registered: 12/7/03
Re: Help binding a TFDMemTable to a TStringGrid at run time  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 19, 2016 6:13 AM   in response to: Haley Winters in response to: Haley Winters
With FireMonkey you must use Live Data Bindigs. But I see you are using VCL.
Then it will be much more simple to use DB-aware controls, like TDBGrid.
Is it acceptable for you ?

--
With best regards,
Dmitry
Haley Winters

Posts: 13
Registered: 3/11/13
Re: Help binding a TFDMemTable to a TStringGrid at run time  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 19, 2016 3:53 PM   in response to: Dmitry Arefiev in response to: Dmitry Arefiev
Hi Dmitry,

I have swapped every instance of StringGrid for DBGrid and i still get a blank grid.

I have the following two lines of code :

ShowMessage(IntToStr(MemTable.RecordCount)); // Shows 2 records
ShowMessage(IntToStr(LinkGridToDataSource.GridControl.ComponentCount)); // This shows Zero?

The first line of code says that I have two records - ok
The second line of code says that LineGridToDataSource does not have the Grid - even though it has been added before?

I am adding the grid by the following code :

LinkGridToDataSource.GridControl:= NewGrid; // 5

I have tried to alter this code by using things like .GridControl.InsertComponent but I don't seem to be getting very far.

I have added this line of code after // 6

LinkGridToDataSource.Active:= True;

and I get the following error "No list control editor available for TDBGrid".

I hope this starts narrowing down where the problem might be...

Best regards,

Haley

Dmitry Arefiev

Posts: 1,406
Registered: 12/7/03
Re: Help binding a TFDMemTable to a TStringGrid at run time
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 20, 2016 2:58 AM   in response to: Haley Winters in response to: Haley Winters
All you need:
* TFDMemTable
* TDataSource
* TDBGrid

Then connect them:
DataSource1.DataSet := FDMemTable1;
DBGrid1.DataSource := DataSource1;


You does not need LineGridToDataSource and other Live Data Binding stuffs.
I am suggesting to you to use DB-aware controls instead of Live Data Binding.

--
With best regards,
Dmitry
Haley Winters

Posts: 13
Registered: 3/11/13
Re: Help binding a TFDMemTable to a TStringGrid at run time  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 21, 2016 1:56 AM   in response to: Dmitry Arefiev in response to: Dmitry Arefiev
Hi Dmitry,

I couldn't just tick this with an "answered" without giving you a reply as well.

You managed to unclutter everything - I couldn't understand why there were so many things and your final solution was the best.

I am now populating the Memtable - I have an idea to do it with two stringlists and I am well on my way to doing it.

Best regards,

Haley.
Code Muser

Posts: 3
Registered: 11/26/12
Re: Help binding a TFDMemTable to a TStringGrid at run time  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 20, 2016 5:06 AM   in response to: Dmitry Arefiev in response to: Dmitry Arefiev
Dmitry Arefiev wrote:
BindSource:= TBindSourceDB(TabSheet); // 1

BindSource:= TBindSourceDB.Create(TabSheet); // 1

--
With best regards,
Dmitry

Also, I think line 6 should be:

BindSource.Dataset:= MemTable; // 6

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02