all groups > dotnet windows forms databinding > january 2005 >
You're in the

dotnet windows forms databinding

group:

Deep databinding - constructing datagrid on the fly



Deep databinding - constructing datagrid on the fly Duncan M Gunn
1/24/2005 10:09:24 AM
dotnet windows forms databinding: Hi,



I would like to bind the following data structure to a datagrid:



List of RowObjects = DataSource


|

|*

RowObject

|

|*

ColObject

(Fields = Name, Value)



I would like the elements of ColObject to display as the columns of the
grid, for each Row object.



That is, when the datagrid asks for the next Row object, the RowObject
examines it's list of child ColObjects, and populates the row accordingly.



ColObjects are initialised at runtime; it is not known in advance how many
Columns there will be in the grid. I would like the datagrid to be
constructed 'on the fly' - unfortunately the solution of using some sort of
adapter object (to convert the above structure into a table) is not
available to me.



E.g.

RowObject 0 =

List of ColObject:



Name Value

---- -----

Col0 A

Col1 1

Col2 3

Col3 7



RowObject 1 =

List of ColObject:



Name Value

---- -----

Col0 B

Col2 4



This should give the following view in the datagrid:



Col0 Col1 Col2 Col3

---- ---- ---- ----



A 1 3 7

B - 4 -





The reason for this is to store the data as efficiently as possible - no
space needs to be allocated for NULL values. If a value does not exist for
a particular Column, then NULL is assumed.



Can I achieve this using IcustomTypeDescriptor/ItypedList?



Has anyone attempted something similar?



Any help/suggestions would be appreciated.



Thanks in advance,

Duncan M. Gunn


Re: Deep databinding - constructing datagrid on the fly Duncan M Gunn
1/26/2005 2:12:05 PM
We managed to solve this problem.

The answer lies with PropertyDescriptor objects. Using the example given in
the previous post, ListOfRowObjects should implement IBindingList,
ITypedList. In GetItemProperties(), create a CellPropertyDescriptor object
for each of the columns represented by the list of ColObjects, initialising
the ColumnIndex property of each.

When the datagrid requests data from the ListofRowObjects, it will iterate
over each PropertyDescriptor first, for each RowObject in its list.
CellPropertyDescriptor.GetValue() is called, which is passed the current
RowObject. RowObject.GetValue() can then be called by
CellPropertyDescriptor, and if it is passed the ColumnIndex property then we
have the (x,y) co-ordinate of the cell being rendered in the grid.
RowObject (or ListOfRowObjects could be delegated this responsibility) can
then work out the value which should be returned.

Hope this is of use to someone.

Now I have to find a way of efficiently storing the Cells. I can't just
create a list of Cell objects for each (x,y) position in the grid, as there
will be several empty Cell objects - the ListOfRowObjects can return a
default value when it is asked for a Cell which is empty for a given (x,y)
value - there is no need to actually store an empty Cell object.

Does anyone know of an efficient storage mechanism for a matrix structure
such as this?

Thanks,
Duncan

AddThis Social Bookmark Button