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"