Hello,
I need to be able to support a grid which at design time will not have a set number of columns. For example, the app will run, get some data from the server and create some objects. Depending on the response from the server, I may have a different number of objects each time I run the app.
What do I need to do to be able to have my grid show the right number of columns and bind them to the right objects? I am guessing this will need to be done in code since it cannot be done in XAML.
Can someone provide a simple exampe of this functionality please?
Thanks.
Hey guys, any tips/samples demonstrating this?
Hi,
I am not sure exactly what you mean, but the grid will automatically generate the columns/fields depending on your data source. You don’t need to specify the columns/fields unless you want to do some customizations to them i.e. to change the label of the column. Hope this helps!
Thanks,
Diyan Dimitrov
Hey Diyan,
Here is an example of what I mean. Lets say I make a call to some service and get back an xml response with some info. I deserialize that response into a number of objects, which can be different each time the call is made.
Lets say each object has two properties, Label and Value. I would like the grid to show columns with labels that match the value of Label with values from Value. So if I have a two objects, obj1 and obj2, that look like this:
obj1.Label = "Parts"
obj1.Value = "17"
obj2.Label = "Parts"
obj2.Value = "12"
I would like a grid that looks like:
Parts
--------
17
12
If I bind my data source to the grid, the grid automatically just uses the object's properties to create the columns, so I see columns of Label and Value:
Label Value
-------------------
Parts 17
Parts 12
I am assuming that I cannot achieve what I want just via xaml. Do you have an example of what I am looking for?
Let me know if I am not clear.
You have to set AutoGenerateFields property to false and add a layout with the fields that you want. Here is a code sample:
xamDataGrid.FieldLayoutSettings = new FieldLayoutSettings(); xamDataGrid.FieldLayoutSettings.AutoGenerateFields = false; xamDataGrid.FieldLayouts.Add(new FieldLayout()); xamDataGrid.FieldLayouts[0].Fields.Add(new Field("Value", obj1.Label));
xamDataGrid.FieldLayoutSettings = new FieldLayoutSettings();
xamDataGrid.FieldLayoutSettings.AutoGenerateFields = false;
xamDataGrid.FieldLayouts.Add(new FieldLayout());
xamDataGrid.FieldLayouts[0].Fields.Add(new Field("Value", obj1.Label));
Hope this helps.