Solved! Go to Solution.
private void QueryTask_ExecuteRelationshipQueryCompleted(object sender, RelationshipEventArgs e) { IEnumerable<Graphic> graphics = e.Result.RelatedRecordsGroup.First().Value; RelationshipResult pr = e.Result; if (pr.RelatedRecordsGroup.Count == 0) { RelatedRowsDataGrid.ItemsSource = null; } else { foreach (var pair in pr.RelatedRecordsGroup) { RelatedRowsDataGrid.ItemsSource = pair.Value; MyFDG.ItemsSource = pair.Value; } } } private void QueryTask_ExecuteRelationshipQueryCompleted(object sender, RelationshipEventArgs e) { //This line gets you the all features returned from the Relationship Query IEnumerable<Graphic> graphics = e.Result.RelatedRecordsGroup.First().Value; // There are two ways to filter either using Where clause or using the ObjectIDs property // before you set the Where clause to return all records 1=1 I think // Here instead lets use the ObjectIDs approach, first get rid of the old Where _featureLayer.Where = null //remember _featureLayer is l //ObjectIDs takes an int[] and with a little Linq we can get that array // I am assumning you have included OBJECTID in your outfields when you defined the query int[] objectIds = graphics.Select(g => (int)g.Attributes["OBJECTID"]).ToArray(); //now set the ObjectIDs on our FeatureLayer _featureLayer.ObjectIDs = objectIds; //Update the FeatureLayer to 'redraw' the new filtered set of Features _featureLayer.Update(); //And I hope this worked. you could rebind the layer to the grid to make sure all is good MyFDG.GraphicsLayer = _featureLayer; }
1.With the FeatureDataGrid1.Bind a FeatureLayer (not the actual FeatureLayer in my map, though) to the grid but initially have the FeatureLayer as null
2.Do the related records query as before but when I get the results back I set the ObjectId property on my bound FeatureLayer and call Update() on the FeatureLayer
3.To add a record I need to create the Graphics object and first initialize all my Attributes to their default values for the data type
4.Then can add the record to the FeatureLayer and it will show up in the Grid
5.Then you just call Save() on the FeatureLayer
6.For updates the user is editing the graphic so just need a call to save no searching like before
FeatureLayer RelateFeatureLayer = new FeatureLayer(); RelateFeatureLayer = null;
Do the relate records query as before(check I had that working earlier)
when I get the results back I set the ObjectId property on my bound FeatureLayer and call Update() on the FeatureLayerWHere should I do this and how?
<esri:Map x:Name="_map" Background="Transparent" WrapAround="true" IsLogoVisible="False"> <esri:ArcGISDynamicMapServiceLayer ID="Ad Soft" Url="http://sdm7/ArcGIS/rest/services/Ad_Soft/MapServer" /> <esri:ArcGISDynamicMapServiceLayer ID="BaseLayer" Url="http://sdm7/arcgis/rest/services/MapOverview/MapServer" /> <!-- FeatureLayer with Url of Related Table --> <esri:FeatureLayer ID="Feature_Layer" Url="http://sdm7/ArcGIS/rest/services/Demos/Demo_RelatedDataEntry/FeatureServer/1" /> </esri:Map>
<!-- RELATED LINK RESOURCE-->
<common:HierarchicalDataTemplate x:Key="TreeViewItemTemplate">
<StackPanel Orientation="Horizontal">
<Ellipse Fill="Transparent" Height="6" Width="6" StrokeThickness="2" Stroke="Black" Margin="0,0,10,0"/>
<TextBlock Text="{Binding Attributes[APN]}" FontWeight="Bold" HorizontalAlignment="Left" FontSize="10"/>
</StackPanel>
</common:HierarchicalDataTemplate>
<!--END RELATED LINK RESOURCE-->
<Border x:Name="RelateResultsDisplay" Background="#77919191" BorderThickness="1" CornerRadius="5"
HorizontalAlignment="Right" VerticalAlignment="Center" Visibility="Collapsed"
Margin="5" Padding="5" BorderBrush="Black">
<Border.Effect>
<DropShadowEffect/>
</Border.Effect>
<Grid x:Name="RelatedRowsGrid" HorizontalAlignment="Right" Background="White"
VerticalAlignment="Top" MinHeight="200" Margin="10" Width="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition MaxHeight="170" Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="Wells: " Margin="4,10,10,6" Foreground="Black" TextWrapping="Wrap"
FontWeight="Bold" FontSize="10" />
<basics:TreeView x:Name="SelectedWellsTreeView" Grid.Row="1" Grid.Column="0" Margin="2"
BorderBrush="Black" BorderThickness="1" ItemsSource="{Binding}"
SelectedItemChanged="SelectedWellsTreeView_SelectedItemChanged"
ItemTemplate="{StaticResource TreeViewItemTemplate}" />
<TextBlock Margin="4,10,60,6" TextWrapping="Wrap" Grid.Column="1"
Text="Tops related to the selected well: "
FontSize="10" FontWeight="Bold" />
<slData:DataGrid x:Name="RelatedRowsDataGrid" AutoGenerateColumns="False" HeadersVisibility="All"
BorderThickness="1" HorizontalScrollBarVisibility="Hidden" Grid.Row="1" Grid.Column="1"
IsReadOnly="False" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
Margin="2">
<slData:DataGrid.Columns>
<slData:DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[PARCELID]}" Header="Parcel Number"/>
<slData:DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[ADDRNMBR]}" Header="Number"/>
<slData:DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[ADDRNAME]}" Header="Name"/>
<slData:DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[GEN_PLAN]}" Header="General Plan"/>
</slData:DataGrid.Columns>
</slData:DataGrid>
<!--<esri:FeatureDataForm x:Name="MyFeatureDataForm"
FeatureLayer="{Binding Path=Layers[AP], ElementName=Map}"
IsReadOnly="False" LabelPosition="Left" />-->
<!--<esriWidgets:FeatureDataGrid Grid.Row="1" x:Name="MyFDG" Height="170"
Map="{Binding ElementName=Map}"
GraphicsLayer="{Binding Layers[RelateFeatureLayer],
ElementName=Map}" Style="{StaticResource FeatureDataGridStyle1}" />-->
<!--<Border x:Name="FeatureDataFormBorder" Visibility="Collapsed"
HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,10,10,0" Width="300" Height="300" >
<Border.Effect>
<DropShadowEffect Color="Black" Direction="-45" BlurRadius="20" Opacity=".75" />
</Border.Effect>
<esri:FeatureDataForm x:Name="RelatedRowsDataGrid"
FeatureLayer="{Binding Path=Layers[RelateFeatureLayer], ElementName=Map}"
IsReadOnly="False" LabelPosition="Left" />
</Border>-->
</Grid>
</Border>
<!--END GRID FOR RELATED (AP) -->
<StackPanel VerticalAlignment="Bottom">
<Button Content="AddRow" Click="Button_Click"/>
<esri:FeatureDataGrid x:Name="MyFDG" Height="250"/>
</StackPanel>
//For FDG -- Jennifer's code
l = new FeatureLayer()
{
ID = "MyLayer",
Url = "http://myserver/ArcGIS/rest/services/Relate/FeatureServer/1",
Mode = FeatureLayer.QueryMode.Snapshot,
Where = "1=1"
};
l.OutFields.Add("*");
l.Initialized += (s, e) =>
{
if (l.InitializationFailure != null)
MessageBox.Show(l.InitializationFailure.Message);
};
l.InitializationFailed += (s, e) =>
{
if (l.InitializationFailure != null)
MessageBox.Show(l.InitializationFailure.Message);
};
l.Initialized += new EventHandler<EventArgs>(l_Initialized);
l.Initialize();
MyFDG.GraphicsLayer = l;
//End for FDG Jennifer's code-
//for Relate
queryTask = new QueryTask("http://myserver/ArcGIS/rest/services/Relate/MapServer/0");
queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
queryTask.ExecuteRelationshipQueryCompleted += QueryTask_ExecuteRelationshipQueryCompleted;
queryTask.Failed += QueryTask_Failed;
//End for relate
//Jennifer's Code to update the feature data grid with a where 1=1 all records
void l_Initialized(object sender, EventArgs e)
{
var l = sender as FeatureLayer;
l.Update();
}
FeatureLayer l;
private void Button_Click(object sender, RoutedEventArgs e)
{
var g = new Graphic();
if (l != null && l.LayerInfo != null && l.LayerInfo.Fields != null)
{
foreach (var f in l.LayerInfo.Fields)
{
g.Attributes[f.Name] = null;
}
}
else
{
//g.Attributes["sf_311_serviceoid"] = 21467;
//g.Attributes["agree_with_incident"] = (Int16)1;
//g.Attributes["DateTime"] = DateTime.UtcNow;
//g.Attributes["cient_ip"] = "unknown";
//g.Attributes["notes"] = string.Format("Added - {0} - local time", DateTime.Now);
g.Attributes["APN"] = 2146700000;
g.Attributes["GEN_PLAN"] = "GPk";
}
l.Graphics.Add(g);
}
//End Jennifer's code
//Relate Code
private void SelectedWellsTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (e.OldValue != null)
{
Graphic g = e.OldValue as Graphic;
g.UnSelect();
g.SetZIndex(0);
}
if (e.NewValue != null)
{
Graphic g = e.NewValue as Graphic;
g.Select();
g.SetZIndex(1);
//Relationship query
RelationshipParameter relationshipParameters = new RelationshipParameter()
{
ObjectIds = new int[] { Convert.ToInt32(g.Attributes[SelectedWellsTreeView.Tag as string]) },
OutFields = new string[] { "PARCELID, APN, ADDRNMBR, ADDRNAME, GEN_PLAN" },
RelationshipId = 0,
OutSpatialReference = Map.SpatialReference
};
queryTask.ExecuteRelationshipQueryAsync(relationshipParameters);
}
}
void QueryTask_ExecuteRelationshipQueryCompleted(object sender, RelationshipEventArgs e)
{
RelationshipResult pr = e.Result;
if (pr.RelatedRecordsGroup.Count == 0)
{
RelatedRowsDataGrid.ItemsSource = null;
}
else
{
foreach (var pair in pr.RelatedRecordsGroup)
{
RelatedRowsDataGrid.ItemsSource = pair.Value;
}
}
}
//End relate
l = new FeatureLayer()
{
ID = "MyLayer",
Url = "http://myserver/ArcGIS/rest/services/Relate/FeatureServer/1",
Mode = FeatureLayer.QueryMode.Snapshot,
Where = "1=1"
};l = Map.Layers["RelatedFeatureLayer"];
<esri:FeatureLayer ID="RelatedFeatureLayer" Url="http://myserver/ArcGIS/rest/services/Relate/FeatureServer/1" />
//Modified initialzed method
l.Initialized += (s, e) =>
{
l.UpdateCompleted += UpdateCompleted;
l.UpdateFailed += UpdateFailed;
l.Update();
};
private void UpdateFailed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show(e.Error.Message);
}
private void UpdateCompleted(object sender, EventArgs e)
{
MessageBox.Show("WooHoo!");
} l.Initialized += (s, e) =>
{
l.UpdateCompleted += UpdateCompleted;
l.UpdateFailed += UpdateFailed;
l.Update();
};void l_Initialized(object sender, EventArgs e)
{
l.Initialized += (s, f) =>
{
l.UpdateCompleted += UpdateCompleted;
l.UpdateFailed += UpdateFailed;
l.Update();
};
// var l = sender as FeatureLayer;
// l.Update();
}