WinForms data binding

Posts   
 
    
EdDotNet
User
Posts: 31
Joined: 30-Mar-2005
# Posted on: 08-Apr-2005 23:19:37   

I am having a problem getting design-time data binding to work properly.

I have added everything to my form per the help file instructions.

I have a colleciton called "pageTemplateCollection1" that I have dragged onto my form.

in the Form_Load method:


...
pageTemplateCollection1 = someParentEntity.PageTemplate;
...

This alone I would think should work but nothing happens.

The only way I can get data to bind is to manually (in code) re-set the .DataSource property on my controls.

ie. dataGrid1.DataSource = pageTemplateCollection1;

Even though my datagrid1 has the collection/members set up properly.

It would be very helpful if you could post a tutorial on design-time databind.

thanks

TheSwiss
User
Posts: 35
Joined: 05-Apr-2005
# Posted on: 09-Apr-2005 00:59:31   

EdDotNet wrote:

I am having a problem getting design-time data binding to work properly.

I have added everything to my form per the help file instructions.

I have a colleciton called "pageTemplateCollection1" that I have dragged onto my form.

in the Form_Load method:


...
pageTemplateCollection1 = someParentEntity.PageTemplate;
...

This alone I would think should work but nothing happens.

The only way I can get data to bind is to manually (in code) re-set the .DataSource property on my controls.

ie. dataGrid1.DataSource = pageTemplateCollection1;

Even though my datagrid1 has the collection/members set up properly.

It would be very helpful if you could post a tutorial on design-time databind.

thanks

That hasn't actually anything to do with databinding but with the fact, that you're assigning references.

I presume that in InitializeComponent() you have

datagrid1.DataSource = pageTemplateCollection1;

That is why the grid looks alright in the designer. Even at runtime you should see the column headers as expected, which is a sign that you have an entitycollection with the correct entityfactory set as datasource of the grid.

Now, when you call

FetchEntityCollection(pageTemplateCollection1);

the objects (entities) inside (the object referenced by the variable) pageTemplateCollection are populated, but the datagrid's reference to the collection object remains unaltered, which makes the rows appear in the grid.

Analogously, when calling Fetch...() with another collection as argument, this collection is populated. If you assign its reference to pageTemplateCollection1, you do not touch the datagrid1.datasource's reference, which still points to the (more or less empty) object, which was instantiated by

this.pageTemplateCollection1 = new YourNamespace.HelperClasses.EntityCollection();

earlier in the designer generated code. So you won't succed without setting

pageTemplateCollection1 = someParentEntity.PageTemplate;
dataGrid1.DataSource = pageTemplateCollection1;
// or directly
dataGrid1.DataSource = someParentEntity.PageTemplate;

in your Form_Load method. Hope I could help. Stefan

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 09-Apr-2005 11:44:39   

Thanks for helping out, Stefan! simple_smile

Frans Bouma | Lead developer LLBLGen Pro
EdDotNet
User
Posts: 31
Joined: 30-Mar-2005
# Posted on: 11-Apr-2005 15:40:42   

TheSwiss wrote:

So you won't succed without setting

pageTemplateCollection1 = someParentEntity.PageTemplate;
dataGrid1.DataSource = pageTemplateCollection1;
// or directly
dataGrid1.DataSource = someParentEntity.PageTemplate;

in your Form_Load method. Hope I could help. Stefan

Thanks. So I have to manually set the DataSource on any control that I want to bind?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 11-Apr-2005 18:57:53   

No, you do that during design time, but you still have to fetch the data. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
EdDotNet
User
Posts: 31
Joined: 30-Mar-2005
# Posted on: 11-Apr-2005 20:12:44   

Otis wrote:

No, you do that during design time, but you still have to fetch the data. simple_smile

I guess that's what I'm missing. What call specifically should I make to fetch that data so that I don't have to the redundant "dataGrid1.DataSource=xxxx" ?

thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 11-Apr-2005 21:15:23   

EdDotNet wrote:

Otis wrote:

No, you do that during design time, but you still have to fetch the data. simple_smile

I guess that's what I'm missing. What call specifically should I make to fetch that data so that I don't have to the redundant "dataGrid1.DataSource=xxxx" ? thanks

There are 2 different things: - Grid setup (or other control setup, let's say it's a grid), done during design time in vs.net. You do this by dragging an entity collection on the form and setting the grid's properties, setup the columns etc. - Fetching data in the entity collection.

The collection dragged onto the form is actually created in the form and usable for data-storage. So all you need to do is in the form's load routine, after InitialiseComponent is called, you simply fetch data into the collection you dragged onto the form. Though, you can also set the grid's DataSource to a different collection, and fill that collection. That's totally up to you, the design of the columns and the collection you're actually binding to the grid are two different things and have no relation to eachother other than that you use one in design mode to set up the grid and the other at runtime to store the data and to bind to the grid.

Frans Bouma | Lead developer LLBLGen Pro
EdDotNet
User
Posts: 31
Joined: 30-Mar-2005
# Posted on: 13-Apr-2005 15:51:17   

Otis wrote:

EdDotNet wrote:

Otis wrote:

No, you do that during design time, but you still have to fetch the data. simple_smile

I guess that's what I'm missing. What call specifically should I make to fetch that data so that I don't have to the redundant "dataGrid1.DataSource=xxxx" ? thanks

There are 2 different things: - Grid setup (or other control setup, let's say it's a grid), done during design time in vs.net. You do this by dragging an entity collection on the form and setting the grid's properties, setup the columns etc. - Fetching data in the entity collection.

The collection dragged onto the form is actually created in the form and usable for data-storage. So all you need to do is in the form's load routine, after InitialiseComponent is called, you simply fetch data into the collection you dragged onto the form. Though, you can also set the grid's DataSource to a different collection, and fill that collection. That's totally up to you, the design of the columns and the collection you're actually binding to the grid are two different things and have no relation to eachother other than that you use one in design mode to set up the grid and the other at runtime to store the data and to bind to the grid.

I guess I am missing the "Fetching data in the entity collection" part. What exactly does the code look like to do that that is different from:


            pageTemplateCollection1 = site.PageTemplate; //fetch call?
            dataGrid1.DataSource = pageTemplateCollection1; //redundant datasource call

To be clear, the data grid DOES NOT BIND if I do not put that ".DataSource =" call in there, even though the DataSource is set at design time.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 13-Apr-2005 20:58:01   

EdDotNet wrote:

Otis wrote:

EdDotNet wrote:

Otis wrote:

No, you do that during design time, but you still have to fetch the data. simple_smile

I guess that's what I'm missing. What call specifically should I make to fetch that data so that I don't have to the redundant "dataGrid1.DataSource=xxxx" ? thanks

There are 2 different things: - Grid setup (or other control setup, let's say it's a grid), done during design time in vs.net. You do this by dragging an entity collection on the form and setting the grid's properties, setup the columns etc. - Fetching data in the entity collection.

The collection dragged onto the form is actually created in the form and usable for data-storage. So all you need to do is in the form's load routine, after InitialiseComponent is called, you simply fetch data into the collection you dragged onto the form. Though, you can also set the grid's DataSource to a different collection, and fill that collection. That's totally up to you, the design of the columns and the collection you're actually binding to the grid are two different things and have no relation to eachother other than that you use one in design mode to set up the grid and the other at runtime to store the data and to bind to the grid.

I guess I am missing the "Fetching data in the entity collection" part. What exactly does the code look like to do that that is different from:


            pageTemplateCollection1 = site.PageTemplate; //fetch call?
            dataGrid1.DataSource = pageTemplateCollection1; //redundant datasource call

To be clear, the data grid DOES NOT BIND if I do not put that ".DataSource =" call in there, even though the DataSource is set at design time.

Of course you have to, you set pageTemplateCollection1 to reference another object, namely site.PageTemplate simple_smile

Frans Bouma | Lead developer LLBLGen Pro
EdDotNet
User
Posts: 31
Joined: 30-Mar-2005
# Posted on: 14-Apr-2005 16:43:02   

Otis wrote:


            pageTemplateCollection1 = site.PageTemplate; //fetch call?
            dataGrid1.DataSource = pageTemplateCollection1; //redundant datasource call

To be clear, the data grid DOES NOT BIND if I do not put that ".DataSource =" call in there, even though the DataSource is set at design time.

Of course you have to, you set pageTemplateCollection1 to reference another object, namely site.PageTemplate simple_smile

So, to be clear, I have to set the .DataSource of the dataGrid, even though it was already set at design time, and there is no other way to do that. Just wanted to confirm.

Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 14-Apr-2005 17:00:34   

EdDotNet wrote:

Otis wrote:


            pageTemplateCollection1 = site.PageTemplate; //fetch call?
            dataGrid1.DataSource = pageTemplateCollection1; //redundant datasource call

To be clear, the data grid DOES NOT BIND if I do not put that ".DataSource =" call in there, even though the DataSource is set at design time.

Of course you have to, you set pageTemplateCollection1 to reference another object, namely site.PageTemplate simple_smile

So, to be clear, I have to set the .DataSource of the dataGrid, even though it was already set at design time, and there is no other way to do that. Just wanted to confirm. Thanks

No smile . Heh, we have a misunderstanding here simple_smile

There are 2 things: 1) you fill the collection you dragged onto the form:


pageTemplateCollection1.GetMulti(null);

2) you fill another collection and set the DataSource property of the grid to that newly filled collection:


            pageTemplateCollection1 = site.PageTemplate; //fetch call?
            dataGrid1.DataSource = pageTemplateCollection1; //redundant datasource call

I hope you see the difference. Don't confuse a variable (pageTemplateCollection1) with a reference (the object it references). So initially pageTemplateCollection1 points to a collection which is bound to the grid. Setting that variable to a different object doesn't make the grid point to another object as well simple_smile

Frans Bouma | Lead developer LLBLGen Pro
EdDotNet
User
Posts: 31
Joined: 30-Mar-2005
# Posted on: 14-Apr-2005 19:51:13   

Otis wrote:

EdDotNet wrote:

Otis wrote:


            pageTemplateCollection1 = site.PageTemplate; //fetch call?
            dataGrid1.DataSource = pageTemplateCollection1; //redundant datasource call

To be clear, the data grid DOES NOT BIND if I do not put that ".DataSource =" call in there, even though the DataSource is set at design time.

Of course you have to, you set pageTemplateCollection1 to reference another object, namely site.PageTemplate simple_smile

So, to be clear, I have to set the .DataSource of the dataGrid, even though it was already set at design time, and there is no other way to do that. Just wanted to confirm. Thanks

No smile . Heh, we have a misunderstanding here simple_smile

There are 2 things: 1) you fill the collection you dragged onto the form:


pageTemplateCollection1.GetMulti(null);

2) you fill another collection and set the DataSource property of the grid to that newly filled collection:


            pageTemplateCollection1 = site.PageTemplate; //fetch call?
            dataGrid1.DataSource = pageTemplateCollection1; //redundant datasource call

I hope you see the difference. Don't confuse a variable (pageTemplateCollection1) with a reference (the object it references). So initially pageTemplateCollection1 points to a collection which is bound to the grid. Setting that variable to a different object doesn't make the grid point to another object as well simple_smile

I understand, I guess the point is that I have to explicity refetch something that I would think is already there, namely "site.PageTemplate." I understand that you need some sort of trigger to bind, as a dataset has "Fill."

so in this case:


pageTemplateCollection1 = site.PageTemplate; //assigment to a property
pageTemplateCollection1.GetMulti(null);

I assume that there is no redundancy because the assignment doesn't actually cause a load operation.

Also to be clear you HAVE to always set the DataSource manually in code. The collection will no bind simply on a "GetMulti()" or other fetch operation, right? thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 15-Apr-2005 10:27:18   

EdDotNet wrote:

Otis wrote:

EdDotNet wrote:

Otis wrote:


            pageTemplateCollection1 = site.PageTemplate; //fetch call?
            dataGrid1.DataSource = pageTemplateCollection1; //redundant datasource call

To be clear, the data grid DOES NOT BIND if I do not put that ".DataSource =" call in there, even though the DataSource is set at design time.

Of course you have to, you set pageTemplateCollection1 to reference another object, namely site.PageTemplate simple_smile

So, to be clear, I have to set the .DataSource of the dataGrid, even though it was already set at design time, and there is no other way to do that. Just wanted to confirm. Thanks

No smile . Heh, we have a misunderstanding here simple_smile

There are 2 things: 1) you fill the collection you dragged onto the form:


pageTemplateCollection1.GetMulti(null);

2) you fill another collection and set the DataSource property of the grid to that newly filled collection:


            pageTemplateCollection1 = site.PageTemplate; //fetch call?
            dataGrid1.DataSource = pageTemplateCollection1; //redundant datasource call

I hope you see the difference. Don't confuse a variable (pageTemplateCollection1) with a reference (the object it references). So initially pageTemplateCollection1 points to a collection which is bound to the grid. Setting that variable to a different object doesn't make the grid point to another object as well simple_smile

I understand, I guess the point is that I have to explicity refetch something that I would think is already there, namely "site.PageTemplate." I understand that you need some sort of trigger to bind, as a dataset has "Fill."

EITHER call GetMulti on pageTemplateCollection1 OR set the grid's DataSource to a NEWLY fetched collection.

so in this case:


pageTemplateCollection1 = site.PageTemplate; //assigment to a property
pageTemplateCollection1.GetMulti(null);

I assume that there is no redundancy because the assignment doesn't actually cause a load operation.

Those 2 statements are 2 different things.

Also to be clear you HAVE to always set the DataSource manually in code. The collection will no bind simply on a "GetMulti()" or other fetch operation, right? thanks

No, please re-read what I wrote in my previous posting. simple_smile 'pageTemplateCollection1' is just a form variable, POINTING to a collection object which is ALSO bound to the grid. Setting that variable to a different value, i.e.: make it reference a DIFFERENT object, doesn't make the grid bind to a different object, that's completely unrelated.

So initially, by the design time code, the grid is bound to the same OBJECT as pageTemplateCollection1 is pointing to (referencing), though that object is empty, you just have to fill it and it's ok. To fill that OBJECT, you have to use the pageTemplateCollection1 variable, as that variable points to the same object. Setting that variable to a different object, i.e. site.PageTemplate, is just setting the variable pageTemplateCollection1 to a different object, not grid.DataSource.

Frans Bouma | Lead developer LLBLGen Pro
EdDotNet
User
Posts: 31
Joined: 30-Mar-2005
# Posted on: 15-Apr-2005 17:08:20   

Otis wrote:

No, please re-read what I wrote in my previous posting. simple_smile 'pageTemplateCollection1' is just a form variable, POINTING to a collection object which is ALSO bound to the grid. Setting that variable to a different value, i.e.: make it reference a DIFFERENT object, doesn't make the grid bind to a different object, that's completely unrelated.

So initially, by the design time code, the grid is bound to the same OBJECT as pageTemplateCollection1 is pointing to (referencing), though that object is empty, you just have to fill it and it's ok. To fill that OBJECT, you have to use the pageTemplateCollection1 variable, as that variable points to the same object. Setting that variable to a different object, i.e. site.PageTemplate, is just setting the variable pageTemplateCollection1 to a different object, not grid.DataSource.

The problem on my end is that I was calling a manager method (in a different class) to create a new colleciton, and "GetMulti" on it. The local collection was being assign to the return value of that method. That must break the .DataSource property because it is a new object at that point, and then the DataSource needs to be set.

Thanks for your help.

Binds:


     adClientCompanyCollection1.GetMulti( null );   

Doesn't Bind:


adClientCompanyCollection1 = adManager.GetAllClientCompanies();

simple_smile