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 60 Views
GridView
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
0
Maya
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,
Maya
the Telerik team

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

0
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"
        Padding="2"
        FontWeight="Bold"
        FontSize="12"
        Template="{StaticResource MyRedDeleteButton}"
        ToolTipService.ToolTip="Delete Entry"
                Click="Button_Click"
        Cursor="Hand" />
</DataTemplate>

In my Main.xaml

<telerik:RadGridView x:Name="ToDoListGridView" Margin="0"
    AreRowDetailsFrozen="True"
    AutoGenerateColumns="False"
    CanUserFreezeColumns="False"
    CanUserReorderColumns="False"
    CanUserResizeColumns="False"
    CanUserSortColumns="False"
    RowIndicatorVisibility="Collapsed"
    DataContext="{Binding Source={StaticResource ToDoData}}"
    ItemsSource="{Binding Collection}"
    ShowGroupPanel="False" IsReadOnly="True">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="ToDoID"
            IsVisible="False"
            DataMemberBinding="{Binding ToDoID}" />
        <telerik:GridViewDataColumn Header="Date"
            MaxWidth="80"
            MinWidth="80"
            Width="80"
            DataMemberBinding="{Binding ToDoEntryDate}" />
        <telerik:GridViewDataColumn Header="Category"
            MaxWidth="110"
            MinWidth="110"
            Width="110"
            DataMemberBinding="{Binding ToDoCategory}" />
        <telerik:GridViewDataColumn Header="Task/Note"
            MaxWidth="400"
            MinWidth="400"
            Width="400"
            DataMemberBinding="{Binding ToDoTask}" />
        <telerik:GridViewDataColumn Header="Status"
            MaxWidth="80"
            MinWidth="80"
            Width="80"
            DataMemberBinding="{Binding ToDoStatus}" />
        <telerik:GridViewDataColumn Header="Priority"
            MaxWidth="75"
            MinWidth="75"
            Width="75"
            DataMemberBinding="{Binding ToDoPriority}" />
        <telerik:GridViewColumn CellTemplate="{StaticResource MyDeleteMiniButton}"
            FooterTextAlignment="Center"
            IsResizable="False"
            IsGroupable="False"
            IsFilterable="False"
            MaxWidth="30"
            MinWidth="30"
            TextAlignment="Center"
            Width="30"
            Background="Transparent"
            HeaderTextAlignment="Center"
            IsSortable="False"
            IsReorderable="False"
            ShowDistinctFilters="False" />
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

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.
Tags
GridView
Asked by
Rob Ainscough
Top achievements
Rank 1
Answers by
Maya
Telerik team
Rob Ainscough
Top achievements
Rank 1
Share this question
or