English

How To: Dynamically load rows in a horizontal datagrid with the query results.

Summary

Instructions provided explain how to update a datagrid dynamically with the query results, without hard-coding attribute names, similar to the Attribute Query Sample.

Procedure

Building on to the existing sample, the datagrid is populated at runtime without any hard-coded column names.

  • The idea is to bind the DataGrid's ItemSource property with query/identify result attributes:

    Code:
    QueryDetailsDataGrid.ItemsSource = selectedFeature.Attributes;


    Here QueryDetailsDataGrid is the DataGrid's x:Name and selectedFeature is the first feature of the resulting FeatureSet of the query task (refer to the Attribute Query Sample for details).
  • Populate the columns at runtime from the results of the query:

    Code:
    var dictionary1 = selectedFeature.Attributes as IDictionary;
    foreach (DictionaryEntry pair in dictionary1)
    {
    var str = "Attributes[" + pair.Key.ToString() + "]";
    QueryDetailsDataGrid.Columns.Add(new DataGridTextColumn
    {
    Header = pair.Key,
    Binding = new Binding(str)
    });
    }

  • Make sure that the XAML for the datagrid has no columns, because columns will be populated at runtime.

    Code:
    <slData:DataGrid x:Name="QueryDetailsDataGrid" AutoGenerateColumns="True" Background="White"/>