I'm trying to create a datagrid with dynamic columns. The datasource is a Collection of ExpandoObject(s). They'all have the same properties.
I'm able to get this using the WPF native data grid as listed below
XAML
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Results}"/>
Code
IEnumerable<IDictionary<string, object>> rows = dataGrid1.ItemsSource.OfType<IDictionary<string, object>>();IEnumerable<string> columns = rows.SelectMany(d => d.Keys).Distinct(StringComparer.OrdinalIgnoreCase);foreach (string text in columns){ // now set up a column and binding for each property var column = new DataGridTextColumn { Header = text, Binding = new Binding(text) }; dataGrid1.Columns.Add(column);}
I'm trying to get the same behavior using XamDataGrid. I'm trying to dynamically add fields to a fieldlayout. This doesnt seem to work. Every ExpandoObject gets rendered as a Key Value Pair (I guess since it implements IDictionary<object,string>)
Is there a way I can get XamDataGrid to layout each property in the ExpandoObject as a field?
Thanks and appreciate your help
Hello,
I have been looking into your description and usually the XamDataGrid does not support Ditionary data sources. Usually UnboundFields can be added without the need of corresponding properties like the with Field, but I am also not sure what would your DataRecords consist of if all your data is converted to Fields. Would you please describe in more details what your scenario is and by providing a small sample of the data you are going to use along with a description how do you want it to appear in the XamDataGrid.
Looking forward to your reply.
Sincerely,
Petar Monov
Developer Support Engineer
Infragistics Bulgaria
www.infragistics.com/support
Thanks for your response. To give a little background, here is what I'm trying to do. My dataset looks like this
I'm trying to display them in a XamDatagrid in the following structure
The number of columns is dynamic based on the # tickers. I've done this in the past by creating a DataTable with multiple columns and then bind to the grid. It works. Now that WPF4 provides support for dynamic object binding, I was trying to achieve the same using the ExpandoObject. I would like to see if I can create a list of ExpandoObjects, bind them to XamDataGrid, and achieve the same effect like that of DataTable, with fewer lines of code. I could use the standard WPF DataGrid as listed above and it works. I'm using XamDataGrid across my application and I don't want to use the WPF DataGrid.
Thanks again and appreciate your help
I have been reading through your description and this is what I understood the first time, but I just wanted to double-check because I cannot think of a possible solution provided by the XamDataGrid’s functionality. I can suggest you create a wrapper class or converter methods to translate your collection of dictionaries into the data table you want, otherwise the XamDataGrid would at least try to create a lot larger number of DataItems for its Records
Hi,
I am just checking if you require any furhter assistance on this matter.
Regards Petar.
Hi Petar,
I have exactly same requirement as mentioned above by Balakv. Would you be able to provide me sample code for Dynamic Columns using ExpandoObject or similar.
Thank you,
Shakti