Getting the datagrid row data from a button click in a DataGridTemplateColumn

3 posts, 1 answers
  1. Tayfun
    Tayfun avatar
    8 posts
    Member since:
    Dec 2018

    Posted 12 Dec 2018 Link to this post

    Hi,

    I have a datagrid which has an "Actions" column containing two actions(Edit and Delete) bound to radbuttons. I'm wondering how to get the row data when I click either of these 2 buttons.

    XAML:

    <telerikGrid:DataGridTemplateColumn HeaderText="Actions">
                                            <telerikGrid:DataGridTemplateColumn.CellContentTemplate>
                                                <DataTemplate>
                                                    <StackLayout>
                                                        <Grid BackgroundColor="Transparent">
                                                            <Grid.RowDefinitions>
                                                                <RowDefinition Height="Auto" />
                                                            </Grid.RowDefinitions>
                                                            <Grid.ColumnDefinitions>
                                                                <ColumnDefinition Width="Auto" />
                                                                <ColumnDefinition Width="Auto" />
                                                            </Grid.ColumnDefinitions>
                                                            <telerikInput:RadButton 
                                                                Grid.Row="0" Grid.Column="0" HeightRequest="40" Text="Edit" Margin="0, 2, 0, 0" 
                                                                VerticalOptions="StartAndExpand" HorizontalOptions="Center" 
                                                                BackgroundColor="CornflowerBlue" TextColor="White" Clicked="editRow"/>
                                                            <telerikInput:RadButton 
                                                                Grid.Row="0" Grid.Column="1" HeightRequest="40" Text="Delete" Margin="0, 2, 0, 0" 
                                                                VerticalOptions="StartAndExpand" HorizontalOptions="Center" 
                                                                BackgroundColor="Red" TextColor="White" Clicked="deleteRow"/>
                                                        </Grid>
                                                    </StackLayout>
                                                </DataTemplate>
                                            </telerikGrid:DataGridTemplateColumn.CellContentTemplate>
                                        </telerikGrid:DataGridTemplateColumn>

     

    C#

    private void editRow(object sender, EventArgs e)
            {
               //implementing logic based on which row's button is clicked 
            }

            private void deleteRow(object sender, EventArgs e)
            {
                //implementing logic based on which row's button is clicked
            }

  2. Answer
    Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1168 posts

    Posted 12 Dec 2018 Link to this post

    Hello Tayfun,

    The BindingContext of the template is the row's data item, therefore you can just read the BindingContext value directly from any of the UI elements in that template.

    Here's an example for a Button click

    private void Button_OnClicked(object sender, EventArgs e)
    {
        var button = sender as RadButton;
     
        // Put a breakpoint here and inspect the value of BindingContext to find the values you want.
        var context = button?.BindingContext;
    }

    Regards,
    Lance | Tech Support Engineer, Sr.
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Tayfun
    Tayfun avatar
    8 posts
    Member since:
    Dec 2018

    Posted 13 Dec 2018 in reply to Lance | Manager Technical Support Link to this post

    Thanks for the answer :)
Back to Top