Search code examples
c#silverlightrowtelerik-grid

Get Clicked Row of GridView


I have an RadGridView and a HyperLink button attached to each row. I want to get the value of column ID on Hyperlink's Click event. I tried this but got NullReferenceException because SelectedItem contains null.

private void OnGetDetailsClick(object sender, RoutedEventArgs e)
{
   ProductDetail productDetail = new ProductDetail();
   Product product = this.grdProductGrid.SelectedItem as Product;
   productDetail.ProductId = product.ProductID
}

Xaml for RadGridView

<telerik:RadGridView HorizontalAlignment="Left" 
                          Height="278" 
                          VerticalAlignment="Top" 
                          Width="1220" 
                          Margin="20,0,0,0" 
                          Name="grdProductGrid"
                          ColumnWidth="*"
                          AutoGenerateColumns="False" >

                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn x:Name="ID" 
                                                DataMemberBinding="{Binding Path=ProductID}" 
                                                Header="ID" 
                                                IsReadOnly="True" 
                                                IsVisible="False"/>


                    <telerik:GridViewDataColumn x:Name="Name" 
                                                DataMemberBinding="{Binding Path=Name}" 
                                                Header="Name" 
                                                IsReadOnly="True" />

                    <telerik:GridViewDataColumn x:Name="Description" 
                                                DataMemberBinding="{Binding Path=Description}" 
                                                Header="Description" 
                                                IsReadOnly="True" />


                    <telerik:GridViewColumn Header="Details" Width="*">
                        <telerik:GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <HyperlinkButton Content="Details" 
                                                     Tag="Hyperlinkbutton" 
                                                     HorizontalAlignment="Center" 
                                                     Click="OnGetDetailsClick"/> 
                                </StackPanel>
                            </DataTemplate>
                        </telerik:GridViewColumn.CellTemplate>
                    </telerik:GridViewColumn>
                  </telerik:RadGridView.Columns>
</telerik:RadGridView>

Whats wrong with my code? How can I access the row on Click event?


Solution

  • You can use CommandParameter to pass a Product to the click event

      <HyperlinkButton Content="Details" 
        Tag="Hyperlinkbutton" 
        HorizontalAlignment="Center" 
       Click="OnGetDetailsClick" CommandParameter={Binding .}/> 
    

    and in your code behind you have

    Product product=((Product)((HyperlinkButton)sender).CommandParameter)