Select to view content in your preferred language

Please help with ExecuteRelationshipQueryAsync results!

531
1
06-22-2011 10:43 AM
DanDong
Deactivated User
Hi everyone,

I want to use identify task on one of my map service which include shape files and related table. I tracked some thread in the forum to write codes performing RelationshipQuery. But I run into the difficulty in understand the results and how to display them in a datagrid. Below is my codes. There is one item in the RelatedRecordsGroup and it has 111 keys. I use messagebox to see the key and value(in red words at the bottom) and they are correct. But I don't know why the data cannot be bind to the datagrid. I got results like this [ATTACH]7458[/ATTACH]
I really appreciate some thoughts or corrections on my codes. Thanks a lot!!


QueryTask queryRelate = new QueryTask(shapfile.url);   //shapefile url is the layer I want to query
queryRelate.ExecuteRelationshipQueryCompleted += queryRelate_ExecuteRelationshipQueryCompleted;
queryRelate.DisableClientCaching = true;
RelationshipParameter relationParameter = new RelationshipParameter();
relationParameter.ObjectIds = new int[] { objectid };   //objectid is received from the identify task on the shape file
relationParameter.OutFields = new string[] { "*" };   //return all the fields in the related table
relationParameter.RelationshipId = 0;      //the related table's ID is 0
queryRelate.ExecuteRelationshipQueryAsync(relationParameter,queryRelate);


private void queryRelate_ExecuteRelationshipQueryCompleted(object sender, RelationshipEventArgs e)
        {
            RelationshipResult pr = e.Result;

            int voteCount = 0;
            foreach (var item in pr.RelatedRecordsGroup)
            {
                voteCount = item.Value.Count();
                MessageBox.Show(Convert.ToString(voteCount));    //message box says "1"
            }
            if (count > 0)
            {
              var idDataGrid = new DataGrid   //generate a datagrid
                {
                    Height = (count == 1 ? 70 : 125), //if there is only one graphic in the layer, height = 70, otherwise, height = 125
                    AutoGenerateColumns = false,
                    IsReadOnly = false,
                    HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
                    VerticalScrollBarVisibility = ScrollBarVisibility.Visible,
                    HeadersVisibility = DataGridHeadersVisibility.Column,
                    Background = new SolidColorBrush(Colors.White),
                    RowStyle = (Style)LayoutRoot.Resources["MyCustomRow"],
                    CanUserSortColumns = true,
                    //ClipboardCopyMode= DataGridClipboardCopyMode.ExcludeHeader                            
                };

                idDataGrid.ItemsSource = pr.RelatedRecordsGroup.Values;
                //Binding resultFeaturesBinding = new Binding("LastResult.Features");
                //resultFeaturesBinding.Source = pr.RelatedRecordsGroup.Values.ElementAt(0).ElementAt(0).Attributes;
                //querytaskPage.QueryDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);
                //idDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);

                idDataGrid.Columns.Clear();

                foreach (var item in pr.RelatedRecordsGroup.Values.ElementAt(0).ElementAt(0).Attributes)
                {
                    DataGridTextColumn dataGridTextColumn = new DataGridTextColumn();
                    Binding binder = new Binding();
                    binder.Path = new PropertyPath(item.Key);
                    dataGridTextColumn.Header = item.Key;
                    binder.Mode = BindingMode.OneWay;
                    dataGridTextColumn.Binding = binder;
                    dataGridTextColumn.IsReadOnly = false;
                    idDataGrid.Columns.Add(dataGridTextColumn);
                }
                MessageBox.Show(pr.RelatedRecordsGroup.Values.ElementAt(0).ElementAt(0).Attributes.ElementAt(0).Key);     //message box says "OBJECTID"
                MessageBox.Show(pr.RelatedRecordsGroup.Values.ElementAt(0).ElementAt(0).Attributes.ElementAt(0).Value.ToString());     //message box says "66901"


0 Kudos
1 Reply
DanDong
Deactivated User
Never mind guys, I figure it out.

In the beginning I really had a hard time on understanding the following results returned from the completed event.

RelatedRecordsGroup
RelatedRecordsGroup.Values
RelatedRecordsGroup.Values.ElementAt(0)
RelatedRecordsGroup.Values.ElementAt(0).ElementAt(0)

In the end I found the differences between them. So I use a List<Graphic> to store the results from related query.
RelationshipResult pr = e.Result;
List<Graphic> gList = new List<Graphic>();  
            for (int k = 0; k < voteCount; k++) //add the graphics to the graphic list
            {
                gList.Add(pr.RelatedRecordsGroup.Values.ElementAt(0).ElementAt(k));
            }


Assign this graphic list to the datagrid.itemsource
idDataGrid.ItemsSource = gList;


Add columns.
for (int i = 0; i < pr.Fields.Count(); i++)
                {
                    DataGridTextColumn dataGridTextColumn = new DataGridTextColumn();
                    Binding binder = new Binding();
                    binder.Path = new PropertyPath("Attributes[" + pr.RelatedRecordsGroup.Values.ElementAt(0).ElementAt(0).Attributes.ElementAt(i).Key + "]");
                    dataGridTextColumn.Header = pr.RelatedRecordsGroup.Values.ElementAt(0).ElementAt(0).Attributes.ElementAt(i).Key;
                    binder.Mode = BindingMode.OneWay;
                    dataGridTextColumn.Binding = binder;
                    dataGridTextColumn.IsReadOnly = false;
                    idDataGrid.Columns.Add(dataGridTextColumn);                  
                }
0 Kudos