Posts Tagged ‘collection’

This looks to be the solution to my current struggle with binding a collection of multiple types of related objects to a grid. (Or a Devexpress XtraGrid!)

Let’s start with an overview of the problem. If you want to create a grid-like user interface in an application, the easiest solution is to use the Microsoft DataGrid class or an off-the-shelf third party grid control such as the Infragistics UltraGrid, and assign the grid’s DataSource property to the data of interest.

This is simple if the data of interest is a DataSet object or other collection designed for easy integration with grids. But it’s not so easy if you want to connect the grid to a collection of application-specific objects. Most grids are designed such that if you assign the DataSource property to an arbitrary list of objects, something reasonable happens. The grid will typically use reflection to get the names and datatypes of the object’s members. But if you want to control which columns are displayed, how the values are formatted, and so on, then this solution is inadequate. And if the objects contain lists of other objects and you want the grid to be able to display the sublists, the problem is even more difficult.

Microsoft’s ITypedList interface provides a solution. It’s even rather elegant and minimal, although you wouldn’t think so from reading the documentation and examples. Underlying this solution is the PropertyDescriptor class, which provides all of the information required to deal with a particular table column, i.e. its datatype, display name, how to get its value given a row-level object, and so on. Specifically this class’s PropertyType property returns the column’s datatype as a Type object, it’s DisplayName property returns the column’s display name as a string, it’s GetValue method takes a row-level object and returns the column’s value, and so on. This is a great building block. All we need to do is provide the grid with a suitable list of property descriptors whenever the grid needs to know how to display a row. That’s where the ITypedList interface comes in. It provides a method called GetItemProperties which returns a list of PropertyDescriptor objects providing the grid with the information it needs.

via Tips for binding grids to hierarchical data using the ITypedList interface » Lab49 Blog.


Read Full Post »

This is the second project I’ve used this for, I think he’s on to something. Thanks Tim!

If you are databinding your custom objects in a Bindinglist of to a DataGridView you will notice that the users can’t sort the rows by clicking on the columnheaders… Unlike an unbound DataGridView, the SortCompare event is not raised. Here is a class that uses IComparer to implement a BindingList that supports Sorting…


Read Full Post »