我已经将 DataSource 设置为 myBindingList 的 datagridview。 列表项实现 INotifyPropertyChanged,因此 datagridview 会自动响应列表中的更改。
现在我必须计算 datagridview 列的一些摘要。
应该在什么时候完成:
- 数据源更改(OnDataSourceChanged)
- 单元格值变化(OnCellValueChanged)
第一个很清楚,但是第二个我有一个小问题。
OnCellValueChanged 当用户通过控件或 on 更改单元格的值时触发:
myDataGridView.Rows[x].Cells[y].Value=newValue;
但是关于:
myBindingList[myInvoice].Property1=newValue;
DataGridView 自动刷新 (INotifyPropertyChanged) 但它不会触发 OnCellValueChanged 事件。
知道如何从我的 DataGridView 中获取此类信息吗? 它必须在 DataGridView 级别完成,因为我正在编写自己的扩展 dgv 的控件。
感谢您的帮助。
最佳答案
我能想到的解决此问题的最佳方法是使用 BindingSource 作为数据源,然后在您的自定义 DataGridView 中引发您自己的事件以响应 BindingSource.ListChanged
事件。
我可能会用这样的东西覆盖 OnDataSourceChanged
:
public event EventHandler CustomCellValueChanged;
protected override void OnDataSourceChanged(EventArgs e)
{
bs = this.DataSource as BindingSource;
if (bs == null)
{
throw new ArgumentException("DataSource must be a BindingSource");
}
bs.ListChanged += new System.ComponentModel.ListChangedEventHandler(bs_ListChanged);
base.OnDataSourceChanged(e);
}
void bs_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
{
if (CustomCellValueChanged != null)
{
CustomCellValueChanged(this, new EventArgs());
}
}
问题是没有办法(我能想到)获得正确的单元格属性,因此您必须重新评估所有列,而不仅仅是包含更改的列。
关于c# - 数据 GridView 和绑定(bind)列表 : How to check if cell value has changed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7711674/