New to Telerik UI for WPF? Download free 30-day trial

Deleting an Entry

GridView allows you to easily delete rows using the Del key or programmatically.

The control doesn't have built-in UI for deleting rows, but it provides a command that allows to do that easily. The RadGridViewCommands.Delete is a RoutedUICommand that can be assigned directly to a button or any other element that supports commands. The command can be executed also manually in code.

Another option to delete rows is to remove the data items from the ItemsSource collection of the RadGridView.

To disable the Del key deletion set the CanUserDeleteRows property of RadGridView to true.

The following examples show how to manually remove items from the ItemsSource.

<StackPanel x:Name="LayoutRoot"> 
    <Button Content="Delete" 
    Click="Button_Click" /> 
    <telerik:RadGridView x:Name="radGridView" 
                     AutoGenerateColumns="False"> 
        <!--...--> 
    </telerik:RadGridView> 
</StackPanel> 
To determine which items are to be removed, use the SelectedItems collection of the RadGridView. You can also use the SelectedItem or the CurrentItem objects, but the SelectedItems collection is more flexible and allows your deleting logic to function even by multiple selection.

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    ObservableCollection<Employee> itemsToRemove = new ObservableCollection<Employee>(); 
 
    foreach (Employee item in this.radGridView.SelectedItems) 
    { 
        itemsToRemove.Add(item); 
    } 
    foreach (Employee item in itemsToRemove) 
    { 
        ((ObservableCollection<Employee>)this.radGridView.ItemsSource).Remove(item); 
    } 
} 
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) 
    Dim itemsToRemove As New ObservableCollection(Of Employee)() 
 
    For Each item As Employee In Me.radGridView.SelectedItems 
        itemsToRemove.Add(item) 
    Next 
    For Each item As Employee In itemsToRemove 
        DirectCast(Me.radGridView.ItemsSource, ObservableCollection(Of Employee)).Remove(item) 
    Next 
End Sub 

When an item is removed from the ItemsSource collection it is also removed from the SelectedItems collection. This means that you cannot directly use the SelectedItems in the same foreach, where the items get removed, because it will result in an exception. This is where the itemsToRemove comes in hand.

Here is an example of a sample deleting logic implemented in the event handler for a Button's Click event.

private void Button2_Click(object sender, RoutedEventArgs e) 
{ 
    if (this.radGridView.SelectedItems.Count == 0) 
    { 
        return; 
    } 
 
    ObservableCollection<Employee> itemsToRemove = new ObservableCollection<Employee>(); 
 
    //Remove the items from the RadGridView 
    foreach (var item in this.radGridView.SelectedItems) 
    { 
        itemsToRemove.Add(item as Employee); 
    } 
    foreach (var item in itemsToRemove) 
    { 
        ((ObservableCollection<Employee>)this.radGridView.ItemsSource).Remove(item as Employee); 
    } 
} 
Private Sub Button2_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) 
    If Me.radGridView.SelectedItems.Count = 0 Then 
        Exit Sub 
    End If 
 
    Dim itemsToRemove As New ObservableCollection(Of Employee)() 
 
    'Remove the items from the RadGridView 
    For Each item In Me.radGridView.SelectedItems 
        itemsToRemove.Add(TryCast(item, Employee)) 
    Next 
 
    For Each item In itemsToRemove 
        DirectCast(Me.radGridView.ItemsSource, ObservableCollection(Of Employee)).Remove(TryCast(item, Employee)) 
    Next 
End Sub 

In this example the implementation manages both the RadGridView and the data storage. Of course it is up to the developer to shape the deleting logic, so it suits the application requirements in the best way.

See Also

In this article