Answer the question
In order to leave comments, you need to log in
How to delete selected row in datagrid?
Please tell me how to delete the selected row in dataGrid using ViewModel. Deletion should happen at the click of a button.
Data to dataGird is bound like this
<DataGrid x:Name="products" CanUserDeleteRows="False"
AutoGenerateColumns="False" VerticalAlignment="Top" Margin="22,0,0,0"
Height="300">
public BindableCollection<Product> products { get; private set;}
public MainViewModel()
{
db = new Context();
products = new BindableCollection<Product>(db.Product);
}
Answer the question
In order to leave comments, you need to log in
In the model, you should have properties: -
a collection of items (Items, you have these products)
- the current selected item (CurrentSelectedItem)
- a command to delete (DeleteRowCommand . I think the simplest implementation of the ICommand interface will suit you, you can find an example yourself, since there are a lot of them on open spaces of these your Internets)
in the same place in the model, file the method for deleting the selected element
public void DeleteCurrentSelected()
{
if (CurrentSelectedItem != null)
Items.Remove(CurrentSelectedItem);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question