This is a migrated thread and some comments may be shown as answers.
Getting the datagrid row data from a button click in a DataGridTemplateColumn
2 Answers 124 Views
This is a migrated thread and some comments may be shown as answers.
Tayfun
Top achievements
Rank 1
Tayfun asked on 12 Dec 2018, 12:38 PM

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 Answers, 1 is accepted

Sort by
0
Accepted
Lance | Manager Technical Support
Telerik team
answered on 12 Dec 2018, 03:13 PM
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
0
Tayfun
Top achievements
Rank 1
answered on 13 Dec 2018, 08:32 PM
Thanks for the answer :)
Asked by
Tayfun
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Tayfun
Top achievements
Rank 1
Share this question
or