Select to view content in your preferred language

Add Rows to datagrid at runtime

1837
2
05-20-2010 06:12 AM
AndrewChap
New Contributor
Hello,

I have a datagrid view that will hold results from varying layers from a map service using querytask/query.

When I get the results, I want to populate the datagridcolumns on the fly as well as adding the new data.

The code I am using yields only empty data row without the feature set's values.

Any suggestions?

Thanks

chap

void queryTask_ExecuteCompleted(object sender, QueryEventArgs e)
        {
            dg.ItemsSource = null;
            dg.Columns.Clear();
            FeatureSet featureSet = e.FeatureSet;
            List<string> fields = new List<string>();
            // add columns
            foreach (var item in featureSet.FieldAliases)   
            {
                fields.Add(item.Value);
                dg.Columns.Add(new DataGridTextColumn()
                {
                    Header = item.Value,
                   Binding = new Binding(item.Value)
                });
            }
            
            dg.ItemsSource = GenerateData(fields, featureSet);

        }
        public IEnumerable<IDictionary> GenerateData(List<string> fields, FeatureSet fs)
        {
            for (var i = 0; i < fs.Features.Count; i++)
            {
                var dict = new Dictionary<string, object>();
                foreach (var field in fields)
                {
                    dict[field] = (fs.Features.Attributes[field]);
                }
                yield return dict;
            }
        }
0 Kudos
2 Replies
dotMorten_esri
Esri Notable Contributor
If you are using the FeatureDataGrid, do not use the ItemsSource. Instead set the GraphicsLayer property to a GraphicsLayer that holds your results.
The other approach is to use Silverlight's core DataGrid, like shown here: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#QueryWithoutMap
Judging from your sample above, your binding expression doesn't look right. Compare to the xaml in the above link.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Hi ChapScribe,

In your code you are supposed to bind to a dictionary. So I suggest you change your code 'Binding = new Binding(item.Value)' by 'Binding = new Binding("[" + item.Value + "]")'.

Note : this is only working with SL4. With previous version, you have to use the Dictionary converter.
0 Kudos