|
-
Nov 8th, 2006, 10:18 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2.0] windows datagrid binding using arraylist or collectionbase class
Hi,
Has anyone using this kind of binding before?
Thanks,
Popskie
-
Nov 8th, 2006, 10:37 PM
#2
Re: [2.0] windows datagrid binding using arraylist or collectionbase class
You simply assign the collection to the DataSource property, just as you would with a DataTable. The objects in the collection must have a public property to correspond to each column you want to display in the grid.
Also, if you're using .NET 2.0 then I strongly suggest that you use the DataGridView in preference to the DataGrid. If you actually are using a DataGridView then I'd ask that you be more careful with your terminology because, while the answer is the same in this case, the two controls are quite different.
-
Nov 9th, 2006, 12:00 AM
#3
Thread Starter
Fanatic Member
Re: [2.0] windows datagrid binding using arraylist or collectionbase class
thats what I did in my code but I dont know what is lacking or error in my code below JM.
VB Code:
private void button1_Click(object sender, System.EventArgs e)
{
henTable ht = new henTable();
ht.Add("1","First Data","Description");
dg1.DataSource = ht;
}
}
class henTable : CollectionBase
{
private container cnt;
public void Add(string key,string objn1,string objn2)
{
cnt = new container();
cnt.Key = key;
cnt.Object1 = objn1;
cnt.Object2 = objn2;
this.List.Add(cnt);
}
}
struct container
{
public string Key;
public string Object1;
public string Object2;
}
-
Nov 9th, 2006, 12:19 AM
#4
Re: [2.0] windows datagrid binding using arraylist or collectionbase class
Note the wording of my previous post:
The objects in the collection must have a public property to correspond to each column you want to display in the grid.
You cannot bind fields, only properties. You should make your fields private and expose them via public properties, as is generally good practice anyway.
-
Nov 9th, 2006, 12:29 AM
#5
Thread Starter
Fanatic Member
Re: [2.0] windows datagrid binding using arraylist or collectionbase class
thanks jms. It work great now.But what is the difference if I not expose and expose using public properties.
-
Nov 9th, 2006, 12:41 AM
#6
Re: [2.0] windows datagrid binding using arraylist or collectionbase class
Properties exist because they offer certain advantages, basically by combining the external behaviour of a field and the internal behaviour of methods. If you want to know more about those advantages then I suggest an expedition to MSDN.
As for data binding, when you bind an object such as a collection to a control it is the objects property descriptors that actually get bound. Your collection will automatically generate property descriptors for the properties of the objects it contains. You could add property descriptors manually for fields if you want to, but why would you?
Take, for example, when you bind a DataTable to a grid. The rows of the table don't have any properties or fields that correspond to the columns of the table. The DataTable class automatically generates property descriptors for each column, thus allowing the data from those columns to be displayed in a bound control.
The .NET Framework does a lot of work for you behind the scenes. Data binding is something that can be taken for granted but think about how much code you'd have to write to get the same level of functionality without it. Property descriptors are a part of that and the fact that they are generated automatically for properties is something to be thankful for.
-
Nov 9th, 2006, 12:49 AM
#7
Thread Starter
Fanatic Member
Re: [2.0] windows datagrid binding using arraylist or collectionbase class
OK JM thank so much for this.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|