mardi 11 juillet 2017

Delete Multiple Rows From WPF Datagrid using Checkbox and Delete button

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>




Aucun commentaire:

Enregistrer un commentaire