How to bind selected columns to a FeatureDataGrid from a GraphicsLayer

2217
8
Jump to solution
01-30-2012 12:49 PM
JianChen
New Contributor III
Here is my situation: I can set the binding of FeatureDataGrid to a GraphicsLayer but I don't want to show all the fields from the GraphicsLayer, I'm wondering if there is a way I can control which columns (fields) to display in the FeatureDataGrid. The easiest way may be specifying the OutFields in a query; but it is not the option for my case since I need the query includes some fields not necessary needed in the FeatureDataGrid. Is it possible? Either XAML or code-behind doesn't matter. Thanks!
0 Kudos
1 Solution

Accepted Solutions
ChristopherHill
Occasional Contributor
 <esri:FeatureDataGrid Grid.Row="2" x:Name="MyDataGrid" AutoGeneratingColumn="MyDataGrid_AutoGeneratingColumn"     Map="{Binding ElementName=MyMap}"     GraphicsLayer="{Binding ElementName=MyMap, Path=Layers.[California]}" />


private void MyDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) {  switch (e.PropertyName)  {   case "OBJECTID":   case "AREA":   e.Column.Visibility = Visibility.Collapsed;   break;  } }


Just wire up the AutoGeneratingColumn event and when each column gets created check the e.PropertyName for the attribute name you don't want then hide the column.

View solution in original post

0 Kudos
8 Replies
JenniferNery
Esri Regular Contributor
If you are using GraphicsLayer, FeatureDataGrid.Columns will be generated based on GraphicsLayer.Graphics.Attributes. If you want Query results to exclude some fields, then don't add them to your GraphicsLayer.Graphics.Attributes.

Something like this:
     foreach (var f in args.FeatureSet.Features)
     {
      var g = new Graphic() { Geometry = Geometry.Clone(f.Geometry) };
      foreach (var a in f.Attributes)
       if(a.Key != "FieldToExclude")
       g.Attributes[a.Key] = a.Value;
      l.Graphics.Add(g);
     }
0 Kudos
JianChen
New Contributor III
If you are using GraphicsLayer, FeatureDataGrid.Columns will be generated based on GraphicsLayer.Graphics.Attributes. If you want Query results to exclude some fields, then don't add them to your GraphicsLayer.Graphics.Attributes.

Something like this:
     foreach (var f in args.FeatureSet.Features)
     {
      var g = new Graphic() { Geometry = Geometry.Clone(f.Geometry) };
      foreach (var a in f.Attributes)
       if(a.Key != "FieldToExclude")
       g.Attributes[a.Key] = a.Value;
      l.Graphics.Add(g);
     }

Hey Jennifer, thanks for your reply! But what I need is select less fields to display in FeatureDataGrid than those retained in the GraphicsLayer.Graphics.Attributes (a result from a query). For example, I may chose all fields return by a query but just display a very few in the FeatureDataGrid. Is this possible? Thanks!
0 Kudos
JianChen
New Contributor III
up, up, up...
0 Kudos
JenniferNery
Esri Regular Contributor
To answer your question, since FDG.Columns is dependent on GraphicsLayer.Graphics.Attributes, you should not add all fields returned by query to your graphic.Attributes. There could be more fields you can exclude here.
Alternatively, simply use query.OutFields to show only fields you want returned in the FDG.
     foreach (var f in args.FeatureSet.Features)
     {
      var g = new Graphic() { Geometry = Geometry.Clone(f.Geometry) };
      foreach (var a in f.Attributes)
       if(a.Key != "FieldToExclude")
       g.Attributes[a.Key] = a.Value;
      l.Graphics.Add(g);
     }
0 Kudos
JianChen
New Contributor III
To answer your question, since   FDG.Columns is dependent on GraphicsLayer.Graphics.Attributes, you should not add all fields returned by query to your graphic.Attributes. There could be more fields you can exclude here.  
Alternatively, simply use query.OutFields to show only fields you want returned in the FDG. 
     foreach (var f in args.FeatureSet.Features)
     {
      var g = new Graphic() { Geometry = Geometry.Clone(f.Geometry) };
      foreach (var a in f.Attributes)
       if(a.Key != "FieldToExclude")
       g.Attributes[a.Key] = a.Value;
      l.Graphics.Add(g);
     }


Thanks Jennifer! But I'm not sure we are talking about the same thing. I think your code is about how to select fields to GraphicsLayer. You can use either graphic.Attributes or more simply use OutFields of a query. I'm fine with whatever fields being selected to GraphicsLayer. What I want to do is just display a subset of fields available in GraphicsLayer to FeatureDataGrid. In another world, there are some fields are valuable to be returned by the GraphicsLayer, but I may not want to show them in the FeatureDataGrid. For example, I may have fields like [ID], [Project_Name], [Source_File], [Link] in the GraphicsLayer but I just want to display [Project_Name] and [Source_File] fields in FeatureDataGrid (of course, the FeatureDataGrid is binding with the GraphicsLayer); is it possible? I need keep the [Link] field in the GraphicsLayer to make a HyperlinkButton but I do not want to display it in FeatureDataGrid. What do you think? Thanks!
0 Kudos
JenniferNery
Esri Regular Contributor
I think we are talking about the same thing 🙂 You want your query to show all fields, I get that. For this reason, you don't want to limit query.OutFields. So the results that are returned will be everything. Since FDG is bound to a GraphicsLayer and determines its columns based on Graphics.Attributes, what you control is what you add to graphic.Attributes. If you need to access other fields that you did not want displayed in FDG, maybe you can use another GraphicCollection to save FeatureSet.Features.
0 Kudos
ChristopherHill
Occasional Contributor
 <esri:FeatureDataGrid Grid.Row="2" x:Name="MyDataGrid" AutoGeneratingColumn="MyDataGrid_AutoGeneratingColumn"     Map="{Binding ElementName=MyMap}"     GraphicsLayer="{Binding ElementName=MyMap, Path=Layers.[California]}" />


private void MyDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) {  switch (e.PropertyName)  {   case "OBJECTID":   case "AREA":   e.Column.Visibility = Visibility.Collapsed;   break;  } }


Just wire up the AutoGeneratingColumn event and when each column gets created check the e.PropertyName for the attribute name you don't want then hide the column.
0 Kudos
JianChen
New Contributor III
 <esri:FeatureDataGrid Grid.Row="2" x:Name="MyDataGrid" AutoGeneratingColumn="MyDataGrid_AutoGeneratingColumn"
    Map="{Binding ElementName=MyMap}"
    GraphicsLayer="{Binding ElementName=MyMap, Path=Layers.[California]}" />


private void MyDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
 switch (e.PropertyName)
 {
  case "OBJECTID":
  case "AREA":
  e.Column.Visibility = Visibility.Collapsed;
  break;
 }
}


Just wire up the AutoGeneratingColumn event and when each column gets created check the e.PropertyName for the attribute name you don't want then hide the column.


Thanks Chris! This is exactly what I'm looking for!
0 Kudos