简体   繁体   English

DataGridView和BindingList:如何检查单元格值是否已更改?

[英]DataGridView & BindingList : How to check if cell value has changed?

I've got datagridview with DataSource set to myBindingList. 我有datagridview,DataSource设置为myBindingList。 Items of list implement INotifyPropertyChanged so datagridview automatically responds to changes in the list. 列表项目实现INotifyPropertyChanged,因此datagridview会自动响应列表中的更改。

Now I have to calculate some summaries of datagridview columns. 现在我必须计算一些datagridview列的摘要。

It should be done when: 它应该在以下时间完成:

  • datasource changes(OnDataSourceChanged) 数据源更改(OnDataSourceChanged)
  • cell value changes(OnCellValueChanged) 单元格值更改(OnCellValueChanged)

First one is clear, but I've got a small problem with the second one. 第一个很清楚,但第二个问题我遇到了一个小问题。

OnCellValueChanged fires when the user changes value of cell by control or on: 当用户通过控制或更改时更改单元格的值时,将触发OnCellValueChanged:

myDataGridView.Rows[x].Cells[y].Value=newValue;

but what about: 但是关于:

myBindingList[myInvoice].Property1=newValue;

DataGridView automatically refreshes (INotifyPropertyChanged) but it doesn't fire OnCellValueChanged event. DataGridView会自动刷新(INotifyPropertyChanged),但它不会触发OnCellValueChanged事件。

Any idea how can I get such info from my DataGridView? 知道如何从我的DataGridView获取此类信息? It has to be done on DataGridView level because I'm writing my own control which extends dgv. 它必须在DataGridView级别上完成,因为我正在编写自己的控件,扩展了dgv。

Thanks for the help. 谢谢您的帮助。

This closest I can think of to solves this is to use a BindingSource as the datasource and then within your custom DataGridView raise your own event in response to the BindingSource ListChanged event. 我能想到的最接近的解决方法是使用BindingSource作为数据源,然后在自定义DataGridView中引发自己的事件以响应BindingSource ListChanged事件。

I'd probably override OnDataSourceChanged something like this: 我可能会覆盖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());
    }
}

The problem with this is that there is no way (I can think of) to get the correct cell properties so you would have to reevaluate all the columns, not just the column that contained the change. 这样做的问题是,我无法(我能想到)获得正确的单元属性,因此您必须重新评估所有列,而不仅仅是包含更改的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM