Delete item with DEL key

4 posts, 0 answers
  1. Alexander
    Alexander avatar
    21 posts
    Member since:
    Aug 2014

    Posted 14 Jul Link to this post

    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....

  2. Alexander
    Alexander avatar
    21 posts
    Member since:
    Aug 2014

    Posted 14 Jul in reply to Alexander Link to this post

    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>

  3. UI for WPF is Visual Studio 2017 Ready
  4. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    281 posts

    Posted 18 Jul Link to this post

    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.
  5. Alexander
    Alexander avatar
    21 posts
    Member since:
    Aug 2014

    Posted 18 Jul in reply to Stefan Nenchev Link to this post

    So easy...thank you very much!
Back to Top