shaun_weston-eagle-co-nz-esridist

Add Data Grid Dynamically

Discussion created by shaun_weston-eagle-co-nz-esridist on Jun 13, 2012
I'm looking at the QueryTask> Spatial Query example and Instead of having the bindings setup in the XAML here:

<DataGrid.Columns>                        
<DataGridTextColumn Binding="{Binding Attributes[STATE_NAME]}" IsReadOnly="True" Width="100" CanUserSort="True" Header="State Name"/>                        
<DataGridTextColumn Binding="{Binding Attributes[SUB_REGION]}" IsReadOnly="True" Width="110" CanUserSort="False" Header="Region"/>                        
<DataGridTextColumn Binding="{Binding Attributes[STATE_FIPS]}" IsReadOnly="True" Width="45" CanUserSort="False" Header="FIPS"/>                        
<DataGridTextColumn Binding="{Binding Attributes[STATE_ABBR]}" IsReadOnly="True" Width="85" CanUserSort="False" Header="Abbreviation"/>                        
<DataGridTextColumn Binding="{Binding Attributes[POP2000]}" IsReadOnly="True" Width="103"  CanUserSort="False" Header="Population 2000"/>                        
<DataGridTextColumn Binding="{Binding Attributes[POP2010]}" IsReadOnly="True" Width="100" CanUserSort="False" Header="Population 2010"/>                    
</DataGrid.Columns>


I would to have these dynamically generated in the C# code behind somehow, but I'm struggling on how to do that:

It would be something like:



DataGridTextColumn dataColumn = new DataGridTextColumn();
dataColumn.Header = "State Name";
dataColumn.Binding = new Binding Attributes[STATE_NAME];
dataGrid.Columns.Add(dataColumn); But not sure how to get the binding right?

The binding is initally setup when the query is initiated:

Binding resultFeaturesBinding = new Binding("LastResult.Features");            
resultFeaturesBinding.Source = queryTask;            
QueryDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);

Outcomes