Friday, January 8, 2010

DataGrid limitation

This week I hit a limitation in the Silverlight DataGrid control. I'm hoping it's just something I've not learned yet with bindings but it does appear that the simplest solution is to display the data in a different manner.

Problem: The data I'm trying to display is essentially a collection of collections of some object. So the rows can be any number, normal for a datagrid, and the columns are also any number. That's not so normal for a datagrid. Usually you have a set number of columns and your rows vary.

Solution: I tried adding columns to the datagrid dynamically, and while this worked just fine (adding them in codebehind) the real issue I hit was how to bind to the underlaying objects. The data is being built dynamically and I tried a couple of formats. I tried a collection of Arrays, and also an ObservableCollection of ObservableCollections. I could bind to the objects but as Bindings in Silverlight have to bind to properties I couldn't come up with a solution presenting the data this way.

My solution as a result has been to display the data in a more traditional manner, with a lists and a datagrid. When you select an item in the list it repopulates the data in the datagrid to show the objects.

If anyone comes across a way to bind a datagrid cell to a collection of collections please let me know.

1 comments:

Miguel Madero said...

Stehpen,

I think the DG in SL3 supports grouping, I'm not sure if this would help.

You could add a RowDetailsView that shows the inner collection

You could add a converter that takes the original DataContext and returns an IEnumerable that will get flatten the collection in the order you want to show it, then if the row is bound to your parent item then you hide certain colums and for the child item you hide others, then set teh width of the parent colums to zero to avoid showing the headers and only show headers of children....