Select to view content in your preferred language

result datagrid for dynamic headers/columns -- spatial query

2359
4
04-05-2011 10:24 AM
SangamLama
Emerging Contributor
Hi all,
I've been following this tutorial for several days now http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SpatialQuery, trying to check if it fits well for my application. I'm getting query results back and all that, but I'm having trouble populating it on the datagrid the way I need

As you can see on the xaml, the datagrid's headers have been hardcoded as such

<slData: DataGrid.Columns>
                        <slData: DataGridTextColumn CanUserSort="True" SortMemberPath="STATE_NAME" Binding="{Binding Attributes[STATE_NAME]}" Header="State Name"/>
                        <slData: DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[SUB_REGION]}" Header="Region"/>
                        <slData: DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[STATE_FIPS]}" Header="FIPS"/>
                        <slData: DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[STATE_ABBR]}" Header="Abbreviation"/>
                        <slData: DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[POP2000]}" Header="Population 2000"/>
                        <slData: DataGridTextColumn CanUserSort="True"  SortMemberPath="POP2007" Binding="{Binding Attributes[POP2007]}" Header="Population 2007"/>
                    </slData: DataGrid.Columns>

For what I'm about to do, however, I don't know what the headers/ number of columns will be. In such cases, how can I populate the header, and then the contents? I know this is more of a SL question, but I thought maybe you guys could think of a solution quicker, assuming you understand the pain of dealing with IValueConverter..

thanks,
0 Kudos
4 Replies
by Anonymous User
Not applicable
The data grid will auto create the columns based on the data you bind to to it if you don't specify them (the feature data grid works the same way).
0 Kudos
SangamLama
Emerging Contributor
The data grid will auto create the columns based on the data you bind to to it if you don't specify them (the feature data grid works the same way).


that was my first guess. but here's what's different between what I want to do vs what autogeneratecolumns does:

If autogenerated, the datagrid will look something like this:
Name Jon Doe
DOB 12/12/2012
Name Jane Doe
DOB 11/11/2011

but I want it to be like
Name       DOB
Jon Doe    12/12/2012
Jane Doe   11/11/2011

something similar to the tutorial..in other words, the header should be on the columns, not rows
0 Kudos
NareshPuripanda
Emerging Contributor
that was my first guess. but here's what's different between what I want to do vs what autogeneratecolumns does:

If autogenerated, the datagrid will look something like this:
Name Jon Doe
DOB 12/12/2012
Name Jane Doe
DOB 11/11/2011

but I want it to be like
Name       DOB
Jon Doe    12/12/2012
Jane Doe   11/11/2011

something similar to the tutorial..in other words, the header should be on the columns, not rows


Hi,

Did U find a solution to this?

Thanks & Regards,
Naresh
0 Kudos
HugoCardenas
Emerging Contributor
You can create a small class (object) which will hold your result values.  You can fill the class at run-time from the IEnumerable<T> set the query returns.  Then, bind the object, directly to the datagrid using ItemsSource property.

public class MyClass
{
  public string Name {get; set;}
  public DataTime DOB {get; set;}
}

/* Create a strong typed collection of the class  */
List<MyClass> myList = new List<MyClass>();

Now, iterate through the IEnumerable<T> collection you get from the query.  Each row can be an instance of "Myclass".

foreach (IEnumerable<Graphic> graphic in your-querry-result)
{
   MyClass myObject = new MyClass();  // one instance for each row
   myObject.Name = graphic.Attributes["Name"];
   myObject.DOB = graphic.Attributes["DOB"];

   /* Add filled class instance (a row) to collection  */
   myList.Add(myObject);
}

/* Bind your strong-type collection to the datagrid  */
myDatagrid.ItemsSource = myList;

Note:
In light of simplicity I hard-coded the column names.  You do not have to, really.  You can use Reflection to match the column name and data type to that of the class "MyClass".  In this scenario, you will need another foreach inner loop to iterate through every column of each row:

foreach (object attribute in graphic.Attributes)
{
  /* now use Reflection to expose every property of "attribute" */
}

If this is your case, I can be more specific about Reflection, if needed.

Hope this helps....
Hugo.
0 Kudos