Wpf Datagrid 删除行问题

标签 wpf datagrid delete-row

我是 WPF 新手。我想从数据网格运行时删除行。当我尝试像这样删除行时

Datagrid.Items.Remove(eRow);

它给我一个错误“错误是:在使用 ItemsSource 时操作无效。改为使用 ItemsControl.ItemsSource 访问和修改元素。”

我在网上看到您可以使用 ObservationCollection 和 InotifyPropertyChangedEvent,但我不知道如何实现它。

我有这样的删除按钮

enter image description here

这是数据网格

    <ctrls:RhinoDataGrid x:Name="dataGrid" Grid.Row="1" Margin="5" ItemsSource="{Binding Model.CurrentDataTable}"   
                             Style="{StaticResource RhinoDataGridBaseStyle}" IsReadOnly="{Binding Model.IsLinkFile}"
                             SelectedValue="{Binding Model.CurrentDataRow}" SelectedValuePath="Row"
                              >

            </ctrls:RhinoDataGrid>

请帮助我。谢谢。

最佳答案

您的 DataGrid 的 ItemsSource 绑定(bind)了 Model.CurrentDataTable。如果要删除一行,则必须删除该集合中的项目。但如果集合未实现 INotifyCollectionChanged,DataGrid 不会注意到该修改.

.NET 有一个内置的 ObservableCollection ,它实现了 INotifyCollectionChanged。如果您将此集合类型用于 CurrentDataTable,则当集合被修改时,DataGrid 将自行更新。

关于Wpf Datagrid 删除行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10564699/

相关文章:

wpf - 数据网格列不更新

apache-flex - Flex - 垂直对齐数据网格中的单元格

python-2.7 - np.delete 和 np.s_。 np_s 有什么特别之处?

mysql - 删除重复的行只留下最旧的行?

c# - PropertyChangedEvent 和 CanExecute 问题

c# - 从自己的后台线程更新绑定(bind)控件

c# - 更改数据网格中特定行的颜色 C# windows ce 紧凑框架

wpf - 如何增加组合框显示项目的填充?

c# - 无法从父 View 模型WPF MVVM应用程序的弹出窗口中获取值

python - 如何删除 pandas 数据框中特定的重复行?