R – Silverlight DataGrid – Binding to a Collection of Collections of objects

bindingdata-bindingdatagridsilverlight

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. The datagrid cell will be either a string or a value changeable via a combobox.

Attempted 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 list and a datagrid. When you select an item in the list it repopulates the data in the datagrid to show the objects.

Question: is there a way to bind a datagrid cell to a collection of collections of objects?

I found this question (WPF) which looks similar and it didn't help any. I think it's the same issue.
WPF DataGrid: DataGridComboxBox ItemsSource Binding to a Collection of Collections

Best Answer

I think I understand what you are trying to accomplish and I actually have a more elegant solution to your problem and it does not involve writing any custom classes. I wrote a blog post on this issue. The blog is geared towards the DataGrid from the Silverlight Toolkit, but you can easily modify it to use any grid.

The solution is here.

Let me know if this is what you were looking for.

Related Topic