|
-
Oct 3rd, 2009, 09:23 PM
#1
Thread Starter
Junior Member
Custom business object databinding with dynamic fields
Hello
Suppose that I have a Field class that has two properties: FieldName and Value. A collection of Field objects would be contained within a Record object.
I would like that when I assign a Record object to the DataSource property of a DataGrid control, then the control would "understand" that it has to iterate through the Field collection in order to know what columns to render -the same way as with a DataTable. Since I am assigning a single Record object, then modifying a cell of the only available row would automatically update the Value property of the corresponding Field object.
How do I have to program the Record class in order to do so?
Thanks a lot.
-
Oct 4th, 2009, 09:19 PM
#2
Re: Custom business object databinding with dynamic fields
I think you're misinterpreting slightly how the DataTable works. You're talking about a Record object behaving like a DataTable, yet your Record class would be equivalent to a DataRow, not a DataTable. In ADO.NET a DataRow represents a single record.
That said, the DataTable is only part of the story when it comes to data-binding. It's actually the DataView class that does all the magic. When you bind to a DataGridView, ComboBox, ListBox or whatever, the data source must implement either the IList or IListSource interface. If it's an IList then the data is taken directly from the object. If it's an IListSource then it's GetList method is called to get an IList and then the data taken from that. The DataTable implements the IListSource interface. When you bind a DataTable its GetList method is called, which returns a DataView. The DataTable is forgotten at that point and the DataView is used for all binding operations.
For the functionality you're asking for I think you'll need to look at the ITypedList interface. I'm not sure though as my basic reading indicates that that is for design-time binding support specifically. I thought it was ICustomTypeInitializer that would be needed, but the DataView class doesn't implement that so I'm not sure. Suffice it to say that it's not a trivial matter and it will involve at least one interface.
That said, does your implementation offer anything over what the DataTable et al already offer? If not then I'd just stick with them.
-
Oct 4th, 2009, 11:07 PM
#3
Thread Starter
Junior Member
Re: Custom business object databinding with dynamic fields
Thanks for your kind reply. In my particular scenario, I want to databind a grid to a single business object -hence the name "Record" for that class. At runtime, the number of fields would change from time to time, so it would provoke that the available columns in the grid would correspondily change as well.
I presume now that is unavoidable to implement IList, even so that my list object would have only a single element. I've been reading about the ITypedList interface and I think that it holds the answer, but I still have to learn how to use it.
I understand that I might improvise using an stand-alone DataTable, but I am taking this as a coding exercise. Besides that, the DataTable class has many features that I do not need for this scenario.
Thanks a lot.
-
Oct 4th, 2009, 11:20 PM
#4
Re: Custom business object databinding with dynamic fields
I'm a little confused as to why you would use a grid if you want to display a single object. Surely one of the main points of using a grid is the ability to display multiple rows.
-
Oct 7th, 2009, 06:49 AM
#5
Thread Starter
Junior Member
Re: Custom business object databinding with dynamic fields
Thanks. By reading this Blog I learnt how to used ITypedList for my scenario:
http://jopinblog.wordpress.com/2007/...ymous-methods/
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
|