问题
Our shop primarily use "ADO objects" (I'm referring to DataSet
, DataTable
, and DataRow
) for marshaling around data and manipulating it on forms and in grids. We are experimenting with creating shop-flavored counterparts to the native ADO objects so that we can impose our in-house standards on top of them.
Implementing this is straightforward with one exception: I want our objects to be bindable to controls.
I'm aware of IList, IBindingList, BindingSource, etc. and have looked at and experimented with these, but I am falling short when it comes to making a class with no hard property names bindable. The native ADO objects are not strongly typed by default. You have column values that you access via the Item
property (e.g. row("Age")
or row.Item("Age")
). The binding examples I've found online deal with strong-typed objects (e.g. row.Age
). Essentially, we want to create custom ADO objects (not inherited from the native ones) that still retain their ability to bind to controls.
Is there a bare-bones example of how this could be implemented?
In a grid I would expect to see columns Name
, Age
and Occupation
from a row where these properties are exposed via row("Name")
, row("Age")
and row("Occupation")
. This must be doable as this is exactly what the native DataTable
and DataRow
provide.
This is a refinement of an earlier question.
回答1:
It seems you need to use the ITypedList interface, which works in conjunction with PropertyDescriptor. The clearest article I have found on this is here:
http://blog.lab49.com/archives/705
I had attempted this myself a while back, and will be giving it another try based on this article.
来源:https://stackoverflow.com/questions/7192379/create-custom-datatable-which-is-bindable-to-winforms-controls