Select to view content in your preferred language

Add a Click and a parameter in the datagrid

621
3
08-12-2011 09:26 AM
DorothyMortenson
Occasional Contributor II
Hello,

I have a datagrid with an attribute called snp_id.

I would like to be able to click on that attribute in the datagrid, take the value of snp_id as a parameter  to do something else.

</sdk:DataGridTemplateColumn>
                                <sdk:DataGridTemplateColumn Header="Zoom to Snap" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto">
                                    <sdk:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <!-- link to snap using the find -->
                                            <HyperlinkButton x:Name="snp_link" 
                                               Click="findSnap_Click"
                                               Content="{Binding Attributes[snp_id]}" 
                                               Tag="{Binding Attributes[snp_id]}" 
                                               VerticalAlignment="Center">      
                                            </HyperlinkButton>
                                        </DataTemplate>
                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>



In the code behind:
private void findSnap_Click(object sender, RoutedEventArgs e)
          {

              MessageBox.Show("you made it to the findsnap function: " + snp_lnk);
              // find_waterright(snp_lnk);
          }


I've also tried the Command instead of Click, but I think I botched it up badly and didn't get it to work.

Thanks for any help you can provide.
Dorothy
0 Kudos
3 Replies
HugoCardenas
New Contributor
First, set the "SelectionChanged" event in your datagrid:

<esri:FeatureDataGrid  x:Name="MyDataGrid" SelectionChanged="MyDataGrid_SelectionChanged" />

Next, capture the graphic clicked from the row in the datagrid:

private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs args)
{
   IEnumerable<ESRI.ArcGIS.Client.Graphic> myGraphics = MyDataGrid.SelectedGraphics;
   int id = Convert.ToInt32(myGraphics.Last<Graphic>().Attributes["snp_id"]);

   /* Now you can pass "id" to other methods or global fields. */
}

Note:
Here I cast it to "integer".  You can cast it to your own type that suites your needs.  You must cast it, however, for "Attributes" values remain of type object.
Also, you may want to check for "null" or count > 0 before issuing those two commands (weird things happen sometimes).

Hugo.
0 Kudos
DorothyMortenson
Occasional Contributor II
I get an error with the .SelectedGraphics;

It says that 'System.Windows.Controls.Datagrid' does not have a SelectedGraphics.

Any suggestions?
0 Kudos
vipulsoni
Occasional Contributor
I get an error with the   .SelectedGraphics;

It says that 'System.Windows.Controls.Datagrid' does not have a SelectedGraphics. 

Any suggestions?


Change your datagrid in xaml to <esri:FeatureDataGrid (instead of using windows controls datagrid)...
0 Kudos