Binding an indexer to the columns of a DataGrid

  • Thread starter Bruce Schechter
  • Start date
B

Bruce Schechter

I have an ArrayList (myArrayList) in which each element is an object of a
custom class called DataRecord. DataRecord only exposes its internal data
in the form of an indexer. During runtime, I need bind myArrayList to a
DataGrid to display all elements of the ArrayList in the DataGrid. The
number of elements of the DataRecord indexer is only known at runtime.

I can of course do this...
dataGrid.DataSource = myArrayList;

But how can I programmatically specify that I want to set the indexer of
DataRecord as the data to be displayed in the columns of the DataGrid? Can
I set the .MappingName property of each column one-by-one somehow?

Thanks,
Bruce
 
Y

Ying-Shen Yu[MSFT]

Hi Bruce,

From your description, my understanding to your problem is you'd like to
bind an arraylist of object DataRecord with a datagrid, the class
DataRecord didn't have property for each column, instead it uses a indexer
to access the internal data. You want to show these columns in the datagrid
just as bind a datagrid to a Table. If my understanding is not correct or
incomplete, please reply this thread to let me know.

Based on my research, data binding in windows forms doesn't support binding
to indexer.
I'd like to know why the DataRecord class only exposes his internal data
through a indexer?
If you could not modify this definition class. Is it possible to add a
wrapper class and define some properties to access the indexer, then add
this wrapper class into the array list?

Please let me know if this workaround works for you,
Thanks

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
B

Bruce Schechter

Hi Ying-Shen,

Yes, your re-statement of my question is correct.

I could modify the DataRecord class. However, the problem is that the
number of of data items that will be displayed in the Datagrid varies at
runtime (depending on data read from an external source). This means that
the number of columns in the bound DataGrid also varies accordingly. A key
design requirement of the DataRecord class is that it must (or other
reasons) expose its internal data via an indexer, and so I had hoped to bind
the indexer directly to the DataGrid (columns).

I suppose I could generate a large sequence of indiviual custom properties,
which would expose the equivalent value of DataRecord[0], DataRecord[1],
etc. But that is a very crude solution, and I could never be sure that I
had provided a high enough number of these individual properties.

Any other suggestions to achieve the same goal? I.e., to enable binding of
DataRecord to the DataGrid in light of the dynamic number of class elements
to find to DataGrid columns?

Thanks,
Bruce
 
Y

Ying-Shen Yu[MSFT]

Hi Bruce,

From your reply, I see simply defining a wrapper class is not a good
workaround for your situation.

To generate the properties at run time, you may try this approach, however
it is a bit more complex and needs more code on it.

1. implement ICustomTypeDescriptor on the DataRecord class, it will allow
you create properties at run-time.
2. Define your own Property Descriptors by deriving the PropertyDescriptor
class. You may find some sample code on how to write a PropertyDescriptor
class in the Technical article
<Make Your Components Really RAD with Visual Studio .NET Property Browser>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/ht
ml/vsnetpropbrow.asp

3. In ICustomTypeDescriptor.GetProperties method, return the property
descriptors on your need.

Note , to enable full edit support in the datagrid (edit mode/abort/commit
state), you need implement IEditableObject interface on the DataRecord
class, also if the datagrid support add/remove records in the collection,
you need implement the IBindingList interface on the collection class to
make the databinding work properly.

If you have any further questions on this issue, please reply this thread
to let me know.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
B

Bruce Schechter

Hello Ying-Shen,
Very interesting. This is a bit advanced for my skillset, but I'm going to
dive and see what I can do with it. I'll let you know if I have any
questions. Thank you very much.
cheers, Bruce
 
Y

Ying-Shen Yu[MSFT]

Hi Bruce,

Besides that article, www.codeproject.net also have lots of articles (as
well as sample codes) on ICustomTypeDescriptor and PropertyDescriptor topic.
I hope they could be helpful to you.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top