Edit this page

Deleting an Entry

The RadGridView has no built-in deleting functionality, but there is no need of such, as it provides you with all the things you need to implement one. This implementation consists of manually removing the desired items from the ItemsSource collection of the RadGridView.

Users can delete (if supported by the grid ItemsSource) selected items using DELETE key. This feature can be enabled by setting the RadGridView's CanUserDeleteRows property to True.

For example, you can have a button in which click event you implement your deleting logic.

<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

Was this article helpful? Yes No

Give article feedback

Tell us how we can improve this article

Dummy