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

Delete item with DEL key

3 Answers 1218 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alexander
Top achievements
Rank 1
Alexander asked on 14 Jul 2016, 07:53 AM

Hi,

I want to achieve that the user can delete the selected item with the DEL key. I have set the 'CanUserDeleteRows' property to true but when I press the DEL key on a selected Item the Deleting, Delete and the KeyDown event aren't fired. I don't want a additional column with delete buttons or a Delete Button beside of the GridView.

Any idea? My search wasn't successfull....

3 Answers, 1 is accepted

Sort by
0
Alexander
Top achievements
Rank 1
answered on 14 Jul 2016, 07:54 AM

Ah here is my XAML Code, sorry

 

<telerik:RadGridView x:Name="gvRecipeDye" Grid.Row="1" Grid.RowSpan="4" Grid.Column="6" ShowGroupPanel="False" IsFilteringAllowed="False"
                     SelectionMode="Single" RowIndicatorVisibility="Collapsed" CanUserInsertRows="True" CanUserDeleteRows="True" CanUserFreezeColumns="False"
                     DataContext="{StaticResource RecipeDyeViewModel}" ItemsSource="{Binding RecipeDye}"
                     Width="300" AutoGenerateColumns="False" NewRowPosition="Bottom" GroupRenderMode="Flat"
                     AddingNewDataItem="gvRecipeDye_AddingNewDataItem" SelectionChanged="gvRecipeDye_SelectionChanged"
                     CellEditEnded="gvRecipeDye_CellEditEnded" SelectionChanging="gvRecipeDye_SelectionChanging" KeyDown="gvRecipeDye_KeyDown">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Name="cColor" Header="Farbe" IsReadOnly="True" CellStyle="{StaticResource GridViewCellStyleColor}" KeyDown="gvRecipeDye_KeyDown"/>
        <telerik:GridViewDataColumn Name="cDyeDescription" Header="Bezeichnung" Width="2*" DataMemberBinding="{Binding DyeName}">
            <telerik:GridViewDataColumn.CellEditTemplate>
                <DataTemplate>
                    <Grid>
                        <telerik:RadComboBox Name="cbDye"
                                             DataContext="{StaticResource DyeViewModel}" ItemsSource="{Binding Dye}" Text="{Binding Name}"
                                             DisplayMemberPath="Name" SelectedValuePath="Id"
                                             SelectionChanged="cbDye_SelectionChanged" Loaded="cbDye_Loaded"/>
                    </Grid>
                </DataTemplate>
            </telerik:GridViewDataColumn.CellEditTemplate>
        </telerik:GridViewDataColumn>
        <telerik:GridViewMaskedInputColumn MaskType="Numeric" Mask="#2,2" Width="1*" Name="cDyeQuantity" Header="Menge (g)"
                                           DataMemberBinding="{Binding DyeQuantity}"/>
        <telerik:GridViewColumn>
            <telerik:GridViewColumn.CellTemplate>
                <DataTemplate>
                    <telerik:RadButton Content="Delete" IsEnabled="True"
                                       Command="telerik:RadGridViewCommands.Delete" CommandParameter="{Binding}" CommandTarget="{Binding ElementName=gvRecipeDye}"/>
                </DataTemplate>
            </telerik:GridViewColumn.CellTemplate>
        </telerik:GridViewColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

0
Stefan Nenchev
Telerik team
answered on 18 Jul 2016, 12:23 PM
Hi Alexander,

Actually, the default behavior of the RadGridView is to delete the selected item on pressing the DEL button. Can you please update me what is the version of the controls that you are using? In the meantime, you can try subscribing to the PreviewKeyDown event of RadGridView and delete the selected item from the items collection:

private void clubsGrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                if (clubsGrid.SelectedItem != null)
                {
                    this.clubsGrid.Items.Remove(clubsGrid.SelectedItem);
                }
            }
        }

Regards,
Stefan Nenchev
Telerik by Progress
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Alexander
Top achievements
Rank 1
answered on 18 Jul 2016, 01:36 PM
So easy...thank you very much!
Tags
GridView
Asked by
Alexander
Top achievements
Rank 1
Answers by
Alexander
Top achievements
Rank 1
Stefan Nenchev
Telerik team
Share this question
or