Populating DataGrid

Aug 28, 2009 at 3:22 PM


I am trying to populate datagrid from a webservice. It returns an array of arrays of objects named Property each containing two properties: Name and Value. Name should become a column name in grid, and Value should go into cell, inner arrays forming rows in the grid. 

I do not know at design time number of columns and their names (both can vary), therefore I cannot use XAML for data binding, and everything must be done in code.

Any help is appreciated.




Aug 28, 2009 at 8:21 PM

You should be able to fairly easily build the DataGrid.Columns collection at runtime.  The first thing you should try is binding DataGrid's ItemsSource to your array to see if the autogeneration of columns feature can interpret it and automatically create the columns for you.  If that doesn't work, I would recommend parsing the array and building a collection of items which the autogeneration feature can interpret (such as a collection of objects of your own custom type which has properties for each field specified in the array).  You can customize which columns get displayed by hooking into the autogeneration events and cancelling or modifying each column as it's created.