Defining Results Datagrid In Code behind

3161
9
09-03-2010 10:55 AM
PaulHuppé
Occasional Contributor
Hi,

I need to be able to dynamically define datagrid columns in Code Behind.  How would you convert the following XAML code in C# in code behind assuming the DataGrid name is MyGrid?

Thanks,
Paul


                            <slData:DataGrid.Columns>
                                <slData:DataGridTextColumn CanUserSort="True" SortMemberPath="NAME" Binding="{Binding Attributes[NAME]}" Header="Abrev."/>
                                <slData:DataGridTextColumn CanUserSort="True" SortMemberPath="MIN_NAME_E" Binding="{Binding Attributes[MIN_NAME_E]}" Header="Province Name"/>
                                <slData:DataGridTextColumn CanUserSort="True" SortMemberPath="MIN_STATUS" Binding="{Binding Attributes[MIN_STATUS]}" Header="Status"/>
                            </slData:DataGrid.Columns>
0 Kudos
9 Replies
DominiqueBroux
Esri Frequent Contributor
Here is the code :

 
MyGrid.Columns.Add(new DataGridTextColumn() { 
  CanUserSort = true,
  SortMemberPath = "NAME",
  Binding = new System.Windows.Data.Binding("Attributes[NAME]"),
  Header = "Abrev."
});
 
MyGrid.Columns.Add(new DataGridTextColumn() { 
.....
0 Kudos
PaulHuppé
Occasional Contributor
Hi Dominique,

That seems to work.  that is, I am not getting any errors during compilation and running the applications.  But, I do not get any data in the grid either, so there is still something wrong.

Here is the application flow so that you know what I am trying to do:


  • I select the layer I want to query from the legend using a radio button that sets a textbox with the layer name

  • When I select the tool desired to make a selection on the map, I check the textbox to determine which service to query and the list of fields to get back. At the same time, I add the required columns to the grid using the code you provided.

  • When I execute a query, the query seems to work, but I get an empty grid containing only the headers and no data rows.


I have attached a picture of the results grid.

Paul
0 Kudos
DominiqueBroux
Esri Frequent Contributor
The ItemsSource property of your datagrid have to be initialized with a collection of graphics.

For example after a query returning a QueryEventArgs you should do:
 
myGrid.ItemsSource = args.FeatureSet.Features;
0 Kudos
PaulHuppé
Occasional Contributor
I added that line in the QueryTask_ExecuteCompleted method which executes after the query is executed and it has no effects.
0 Kudos
PaulHuppé
Occasional Contributor
Here is how I modified the Selection Sample from the Silverlight samples web site:

XAML code:
You can see the commented section which is being replace with the column definitions in the code behind in the next code block.  If I remove the comment tags and comment out the code behind to define the columns, everything works.  But of course, I want to be able to do this in code behind.
                        <slData:DataGrid x:Name="QueryDetailsDataGrid" Grid.Row="1"
                                        Width="Auto" Height="120"
                                        AutoGenerateColumns="False" HeadersVisibility="Column" Background="White" 
                                        IsReadOnly="True" HorizontalScrollBarVisibility="Hidden"
                                        RowStyle="{StaticResource MyCustomRow}" CanUserSortColumns="True"
                                        SelectionChanged="QueryDetailsDataGrid_SelectionChanged"
                                        LoadingRow="QueryDetailsDataGrid_LoadingRow">
                            <!--<slData:DataGrid.Columns>
                                <slData:DataGridTextColumn CanUserSort="True" SortMemberPath="NAME" Binding="{Binding Attributes[NAME]}" Header="Abrev."/>
                                <slData:DataGridTextColumn CanUserSort="True" SortMemberPath="MIN_NAME_E" Binding="{Binding Attributes[MIN_NAME_E]}" Header="Province Name"/>
                                <slData:DataGridTextColumn CanUserSort="True" SortMemberPath="MIN_STATUS" Binding="{Binding Attributes[MIN_STATUS]}" Header="Status"/>
                            </slData:DataGrid.Columns>-->
                        </slData:DataGrid>



Code behind:

            // Setup depending on selected layer
            switch (tbxLayerSelected.Text.ToString())
            {
                case "Provinces":
                    // Provinces is the 4th layer, index 3
                    queryTaskService = serverURL + "BaseMap/MapServer/3";
                    fieldsList = new string[] { "NAME", "MIN_NAME_E", "MIN_STATUS" };
                    QueryDetailsDataGrid.Columns.Clear();
                    DataGridTextColumn dgc = new DataGridTextColumn();
                    dgc.CanUserSort = true;
                    dgc.SortMemberPath = "NAME";
                    dgc.Binding = new System.Windows.Data.Binding("Attribute[NAME]");
                    dgc.Header = "Abrev.";
                    QueryDetailsDataGrid.Columns.Add(dgc);
                    dgc = new DataGridTextColumn();
                    dgc.CanUserSort = true;
                    dgc.SortMemberPath = "MIN_NAME_E";
                    dgc.Binding = new System.Windows.Data.Binding("Attribute[MIN_NAME_E]");
                    dgc.Header = "Province Name";
                    QueryDetailsDataGrid.Columns.Add(dgc);
                    dgc = new DataGridTextColumn();
                    dgc.CanUserSort = true;
                    dgc.SortMemberPath = "MIN_STATUS";
                    dgc.Binding = new System.Windows.Data.Binding("Attribute[MIN_STATUS]");
                    dgc.Header = "Status";
                    QueryDetailsDataGrid.Columns.Add(dgc);
                    
                    break;
                default:
                    break;
            }

            //QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
            //    "Demographics/ESRI_Census_USA/MapServer/5");
            QueryTask queryTask = new QueryTask(queryTaskService);
            queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
            queryTask.Failed += QueryTask_Failed;

            // Bind data grid to query results
            Binding resultFeaturesBinding = new Binding("LastResult.Features");
            resultFeaturesBinding.Source = queryTask;
            QueryDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);
            Query query = new ESRI.ArcGIS.Client.Tasks.Query();
            
            // Specify fields to return from query
            //query.OutFields.AddRange(new string[] { "STATE_NAME", "SUB_REGION", "STATE_FIPS", "STATE_ABBR", "POP2000", "POP2007" });
            query.OutFields.AddRange(fieldsList);
            query.Geometry = args.Geometry;

            // Return geometry with result features
            query.ReturnGeometry = true;

            queryTask.ExecuteAsync(query);
        }

        private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
        {
            FeatureSet featureSet = args.FeatureSet;
            if (featureSet == null || featureSet.Features.Count < 1)
            {
                MessageBox.Show(msgNoFeatures);
                return;
            }

            GraphicsLayer graphicsLayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;

            if (featureSet != null && featureSet.Features.Count > 0)
            {
                QueryDetailsDataGrid.ItemsSource = args.FeatureSet.Features;
                foreach (Graphic feature in featureSet.Features)
                {
                    feature.Symbol = LayoutRoot.Resources["ResultsFillSymbol"] as FillSymbol;
                    graphicsLayer.Graphics.Insert(0, feature);
                }
                ResultsDisplay.Visibility = Visibility.Visible;
            }
            MyDrawSurface.IsEnabled = false;
        }
0 Kudos
DominiqueBroux
Esri Frequent Contributor
dgc.Binding = new System.Windows.Data.Binding("Attributes[NAME]");


's' missing.:)
0 Kudos
PaulHuppé
Occasional Contributor
Silly me, it had to be something stupid!  It works now.

thanks for spotting that!

Paul
0 Kudos
EricPfirman
New Contributor III
I used the examples in this thread to successfully bind a Silverlight DataGrid to a FeatureSet in code behind as follows:

MyDataGrid.ItemsSource = featureSet.Features;

Now I would like to do the same with a Silverlight Toolkit DataForm. If I use the following:

ToolkitDataForm.ItemsSource = featureSet.Features;

the DataForm gets bound to Geometry, Symbol, Addtributes, MapTip, etc. and not the attribute fields.

Any suggestions?
0 Kudos
JenniferNery
Esri Regular Contributor
If your ItemsSource is GraphicCollection, then each item is a Graphic. You need to update your Binding to "{Binding Attributes[FieldName]}".
0 Kudos