0

I have a DataGrid that contains a checkbox. I would like to delete all the rows from the DataGrid where the checkbox is checked by clicking on a delete button. The XAML for my datagrid is shown below.

    <DataGrid x:Name="orders" Grid.Row="4" Background="AliceBlue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False"  >
        <DataGrid.Columns>
            <DataGridCheckBoxColumn  Header="Select" Binding="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}}" ></DataGridCheckBoxColumn>
            <DataGridTextColumn Header="Item Name" Binding="{Binding Path=Name}" ></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

I would like to write code for click button to delete all the rows where the checkbox is checked like so.

    private void btnDelete_Click(object sender, RoutedEventArgs e)
    {
        //Delete logic here

    }

How do I achieve this? NB: I'm using EntityFramework and not ADO. the datagrid is bound to an observable collection of type Order like ObservableCollection<Order>

2
  • What is the current behavior based on what you have tried as apposed to what is the expected behavior. If the check box is bound properly then it is just a matter of getting the SelectedItems and removing them from the collection. Commented Jul 12, 2017 at 2:17
  • Maybe a code snippet can help. How do I get the rows that are checked. Commented Jul 20, 2017 at 3:42

3 Answers 3

2

First make sure the DataGrid allows multi-selection via the SelectionMode

SelectionMode="Extended"

Then on button click get the selected items from the grid based on the IsSelected binding in the view

private void btnDelete_Click(object sender, RoutedEventArgs e) {
    //get selected items
    if (orders.SelectedItems != null && orders.SelectedItems.Count > 0) {
        var toRemove = orders.SelectedItems.Cast<Order>().ToList();
        //Delete logic here
        //...remove items from EF and save

        //Once confirmed remove from items source
        var items = orders.ItemsSource as ObservableCollection<Order>;
        if (items != null) {
            foreach (var order in toRemove) {
                items.Remove(order);       
            } 
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
Write code behind like this....
        public MainWindow()
        {
            InitializeComponent();
            List<Great> p = new List<Great>();
            p.Add(new Great() { Name = "Good" });
            p.Add(new Great() { Name = "Bad" });
            p.Add(new Great() { Name = "Ugly" });
            orders.ItemsSource = p;

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<Great> SelectedOrders = new List<Great>();
            for (int i = 0; i < orders.SelectedItems.Count; i++)
            {
                SelectedOrders.Add((Great)orders.SelectedItems[i]);
            }
            var kk = orders.ItemsSource as List<Great>;
            foreach (var item in SelectedOrders)
            {
                kk.Remove(item);
            }
            orders.Items.Refresh();
        }

       public class Great
        {
            public string Name { get; set; }
        }

Comments

0

Just call the Clear() method on the ObservableCollection<Order>:

private void btnDelete_Click(object sender, RoutedEventArgs e)
{
    var sourceCollection = orders.ItemsSource as ObservableCollection<Order>;
    if (orders != null)
        orders.Clear();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.