Roll your own datatable

Laughing.John

New member
Joined
Aug 25, 2006
Messages
3
Programming Experience
10+
I'm a bit confused about creating my own objects for complex databinding.

I understand the you can use a collection of objects or roll your own collection by inheriting from IBindingList.

But suppose I don't have an object and I want to bind to a set of rows with a set of columns that are defined at run-time.

Basically it is the same functionality as a datatable with its datarows. What interfaces do you need to support to roll your own datatable ?

I understand that the Table would use IBindingList or IList, but what about the datarow ? How does a bound control know where to get the values and names of the columns ? Using Reflector I can't work out which interface DataRow supports that allow this ?

Can anyone help ?

The structure looks like this:

Table
Rows
Columns

And it's the collection of columns I don't know how to create. Does this need to also support and IBindingList ? And what sort of interface do the actual fields need to support ?
 
The datarow class doesn't implement any interfaces at all. But the DatarowCollection class does but only what it inherits from InternalDataCollectionBase

ICollection, IEnumerable

These have nothing to do with the Databinding of controls, as you would expect.

How does a table know where to it's column names etc..?

This is all done by the dataadapter/tableadapter on the fill method. In the same way you would manually create columns and rows in the designer or code this is what that method does. It retrieves the column names, number of columns and adds each column to the datatables column collection, same with the rows. Databinding at it's simplest is just a listener for events that are raised by the datarow, datacolumn it responds to these events via the BindingManagerBase class that has two derived classes CurrencyManager and PropertyManager. You can learn more about databinding here....

http://www.codeproject.com/vb/net/databindingconcepts.asp
http://www.15seconds.com/issue/040908.htm

Creating a collection is easy. You would Start something like this.


VB.NET:
<System.Reflection.DefaultMember("Item")> _
Public class MyCollection
Inherits CollectionBase
 
Public sub New(byval Owner as Whatever Object)
.....
End sub
 
Public Sub New
 
End sub
 
 
 
Public Sub(byval Value as MyTypeOfItem) 
Mybase.innerlist.add(value)
'May need to do a type check first
End Sub
And you would carry on creating all the functions/subs you think your collection will use. Of course you will first need to create an actual item to add to your collection.
 
Hi vis781,

Thanks for the reply.

Maybe I need to explain a bit more. I'm trying to bind to a grid. I realise that it is pretty easy to bind to a collection of objects, where the collection is effectively the rows and the properties of the object become the columns.

I want to create a collection of 'rows' where the 'columns' (properties) are dynamic (i.e. they're built at runtime), so I can't just use a collection of fixed objects.

Obviously the datatable does the same thing - the columns can vary depending on the SQL/runtime code used to fill it.

What I'm wondering is when you bind a datatable to a control (e.g. a grid), how does the control know the column names/values. It must query the datatable (bound source) to get this information and therefore I presume it supports some sort of standard interface/method to retrieve this information.

I can't see this on IBindingList, so where is it ?

I realise I'm probably being stupid, so thanks for your patience.

LJ.
 
As best as i can figure it uses PropertyDescriptors to get those values. The metadata that is retrieved from the database also plays a part in this
 
Back
Top