C# DataGridView&;BindingList:如何检查单元格值是否已更改?

C# DataGridView&;BindingList:如何检查单元格值是否已更改?,c#,winforms,datagridview,C#,Winforms,Datagridview,我已将数据源设置为myBindingList的datagridview。 列表项实现INotifyPropertyChanged,以便datagridview自动响应列表中的更改 现在我必须计算datagridview列的一些摘要 应在以下情况下进行: 数据源更改(OnDataSourceChanged) 单元格值更改(OnCellValueChanged) 第一个很清楚,但第二个有点小问题 OnCellValueChanged在用户通过控件或on更改单元格值时激发: myDataGridV

我已将数据源设置为myBindingList的datagridview。 列表项实现INotifyPropertyChanged,以便datagridview自动响应列表中的更改

现在我必须计算datagridview列的一些摘要

应在以下情况下进行:

  • 数据源更改(OnDataSourceChanged)
  • 单元格值更改(OnCellValueChanged)
第一个很清楚,但第二个有点小问题

OnCellValueChanged在用户通过控件或on更改单元格值时激发:

myDataGridView.Rows[x].Cells[y].Value=newValue;
但是关于:

myBindingList[myInvoice].Property1=newValue;
DataGridView会自动刷新(INotifyPropertyChanged),但不会在CellValueChanged事件中激发

你知道如何从DataGridView中获取这些信息吗? 它必须在DataGridView级别上完成,因为我正在编写扩展dgv的自己的控件


感谢您的帮助。

我能想到的最接近解决此问题的方法是使用BindingSource作为数据源,然后在自定义DataGridView中引发您自己的事件以响应BindingSource ListChanged事件

我可能会更改如下内容:

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());
    }
}
这样做的问题是,无法(我能想到)获得正确的单元格属性,因此您必须重新计算所有列,而不仅仅是包含更改的列