This is a migrated thread and some comments may be shown as answers.

Code behind for Button click on GridViewColumn based on CellTemplate?

2 Answers 65 Views
This is a migrated thread and some comments may be shown as answers.
Rob Ainscough
Top achievements
Rank 1
Rob Ainscough asked on 16 Nov 2011, 08:39 PM
I'm trying to determine how to write some code behind (VB) that will handle a Button click event from a GridViewColumn that has a Button as it's content (via CellTemplate).

I have a RadGridView (currently bound to some SampleData created via Blend 4, XAML/XSD) and one of the columns is defined as a GridViewColumn with CellTempate = {StaticResource MiniDeleteButton} which is just a small red button.  The intent is for the user to click the red button on their row of choice and that row gets deleted (with a confirmation "are you sure" message).

Can someone point me in a direction where I can figure how to achieve this.

Thanks, Rob.

2 Answers, 1 is accepted

Sort by
Telerik team
answered on 17 Nov 2011, 10:03 AM
Hi Rob Ainscough,

You can try to bind the command property of the button to the built-in one - Delete. Please take a look at our online documentation and demos for a reference. You can also create a custom one in your ViewModel and bind it again to the Command property.
Another possible way to go would be directly to add a subscribe to the Click event of the button and handle it in the code-behind.

Kind regards,
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Rob Ainscough
Top achievements
Rank 1
answered on 14 Dec 2011, 08:06 PM
I've looked at the documentation and the Demo and I'm not having any success using Code Behind with a button column (cell template).

In my App.xaml:

<DataTemplate x:Key="MyDeleteMiniButton">
    <Button Content="X"
        Template="{StaticResource MyRedDeleteButton}"
        ToolTipService.ToolTip="Delete Entry"
        Cursor="Hand" />

In my Main.xaml

<telerik:RadGridView x:Name="ToDoListGridView" Margin="0"
    DataContext="{Binding Source={StaticResource ToDoData}}"
    ItemsSource="{Binding Collection}"
    ShowGroupPanel="False" IsReadOnly="True">
        <telerik:GridViewDataColumn Header="ToDoID"
            DataMemberBinding="{Binding ToDoID}" />
        <telerik:GridViewDataColumn Header="Date"
            DataMemberBinding="{Binding ToDoEntryDate}" />
        <telerik:GridViewDataColumn Header="Category"
            DataMemberBinding="{Binding ToDoCategory}" />
        <telerik:GridViewDataColumn Header="Task/Note"
            DataMemberBinding="{Binding ToDoTask}" />
        <telerik:GridViewDataColumn Header="Status"
            DataMemberBinding="{Binding ToDoStatus}" />
        <telerik:GridViewDataColumn Header="Priority"
            DataMemberBinding="{Binding ToDoPriority}" />
        <telerik:GridViewColumn CellTemplate="{StaticResource MyDeleteMiniButton}"
            ShowDistinctFilters="False" />

In VB code behind, I'm unable to get a reference to MyDeleteMiniButton click event?

I tried using the Command approach, but that generates all kinds of errors in my App.xaml and prevents my application from running.

Any other suggestions to get this to work?

Thanks, Rob.
Asked by
Rob Ainscough
Top achievements
Rank 1
Answers by
Telerik team
Rob Ainscough
Top achievements
Rank 1
Share this question