我如何在datagrid wpf中删除所选行?

时间:2022-07-13 10:38:29

I am using WPF datagrid I need to delete selected Row , my code is

我正在使用WPF datagrid我需要删除选中的Row,我的代码是

private void dataGridView1_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Delete)
    {
         this.dataGridView1.Items.Remove(this.dataGridView1.SelectedItem);
    }
} 

But when I using this code show me error

但是,当我使用此代码时显示错误

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead

使用ItemsSource时,操作无效。使用ItemsControl.ItemsSource访问和修改元素

How I can Delete Selected Row ?

我如何删除所选行?

4 个解决方案

#1


6  

You never have to delete the row from the WPF grid. What you have to do is:

您永远不必从WPF网格中删除该行。你要做的是:

1) define a type with a ObservableCollection property that contains a list of objects presenting the values on your grid.

1)使用ObservableCollection属性定义一个类型,该属性包含在网格上显示值的对象列表。

2) Bind that property to your grid control.

2)将该属性绑定到您的网格控件。

3) now if you add/remove objects from binded collection, corresponding rows will respectively add/remove from control's ui.

3)现在如果你从绑定集合中添加/删除对象,相应的行将分别从控件的ui添加/删除。

#2


2  

Is guess your DataGrid is bound to an ItemsSource (e.g. an ObservableCollection). In that case manipulating the ItemsSource from the View is not allowed and you rather have to remove it in the ViewModel (that is where your bound objects are stored).

猜猜您的DataGrid绑定到ItemsSource(例如ObservableCollection)。在这种情况下,不允许从View中操作ItemsSource,而您必须在ViewModel中删除它(这是存储绑定对象的位置)。

#3


2  

I think you are using a itemSource to populate the dataGridview. Remove the item from the datasource and then refresh the binding.

我认为您正在使用itemSource来填充dataGridview。从数据源中删除该项,然后刷新绑定。

Or have your datasource class inherit from INotifyPropertyChanged and raise a PropertyChanged event and on the listbox XAML set the UpdateSourceTrigger as the PropertyChanged event, such as below:

或者让您的数据源类继承INotifyPropertyChanged并引发PropertyChanged事件,并在列表框XAML上将UpdateSourceTrigger设置为PropertyChanged事件,如下所示:

ItemsSource="{Binding MyListItems, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}

#4


1  

As clearly mentioned in the error description for a UI control bound to a DataSource you should be manipulating the data source itself and not the UI control ( in this case your data grid ).

正如在绑定到DataSource的UI控件的错误描述中明确提到的那样,您应该操纵数据源本身而不是UI控件(在本例中是数据网格)。

The UI Control is only a way to present your data in the User Interface, to show edited or new or modified data ( for example 1 row less ) you should simply act on the underlying data source you have assigned to the DataGrid's ItemSource property.

UI控件只是一种在用户界面中显示数据的方式,用于显示已编辑的或新的或修改的数据(例如,少一行),您只需对已分配给DataGrid的ItemSource属性的基础数据源执行操作。

#1


6  

You never have to delete the row from the WPF grid. What you have to do is:

您永远不必从WPF网格中删除该行。你要做的是:

1) define a type with a ObservableCollection property that contains a list of objects presenting the values on your grid.

1)使用ObservableCollection属性定义一个类型,该属性包含在网格上显示值的对象列表。

2) Bind that property to your grid control.

2)将该属性绑定到您的网格控件。

3) now if you add/remove objects from binded collection, corresponding rows will respectively add/remove from control's ui.

3)现在如果你从绑定集合中添加/删除对象,相应的行将分别从控件的ui添加/删除。

#2


2  

Is guess your DataGrid is bound to an ItemsSource (e.g. an ObservableCollection). In that case manipulating the ItemsSource from the View is not allowed and you rather have to remove it in the ViewModel (that is where your bound objects are stored).

猜猜您的DataGrid绑定到ItemsSource(例如ObservableCollection)。在这种情况下,不允许从View中操作ItemsSource,而您必须在ViewModel中删除它(这是存储绑定对象的位置)。

#3


2  

I think you are using a itemSource to populate the dataGridview. Remove the item from the datasource and then refresh the binding.

我认为您正在使用itemSource来填充dataGridview。从数据源中删除该项,然后刷新绑定。

Or have your datasource class inherit from INotifyPropertyChanged and raise a PropertyChanged event and on the listbox XAML set the UpdateSourceTrigger as the PropertyChanged event, such as below:

或者让您的数据源类继承INotifyPropertyChanged并引发PropertyChanged事件,并在列表框XAML上将UpdateSourceTrigger设置为PropertyChanged事件,如下所示:

ItemsSource="{Binding MyListItems, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}

#4


1  

As clearly mentioned in the error description for a UI control bound to a DataSource you should be manipulating the data source itself and not the UI control ( in this case your data grid ).

正如在绑定到DataSource的UI控件的错误描述中明确提到的那样,您应该操纵数据源本身而不是UI控件(在本例中是数据网格)。

The UI Control is only a way to present your data in the User Interface, to show edited or new or modified data ( for example 1 row less ) you should simply act on the underlying data source you have assigned to the DataGrid's ItemSource property.

UI控件只是一种在用户界面中显示数据的方式,用于显示已编辑的或新的或修改的数据(例如,少一行),您只需对已分配给DataGrid的ItemSource属性的基础数据源执行操作。