2011-10-10 64 views
5

我有datagrid設置爲myBindingList datagridview。 列表項的實現INotifyPropertyChanged使datagridview自動響應列表中的更改。DataGridView&BindingList:如何檢查單元格值是否已更改?

現在我必須計算一些datagridview列的摘要。

它應該做的事的時候:

  • 數據源的變化(OnDataSourceChanged)
  • 單元格值更改(OnCellValueChanged)

第一個是明確的,但我有一個小問題,第二個。

OnCellValueChanged火災時,用戶通過控制或改變細胞的價值:

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

但什麼:

myBindingList[myInvoice].Property1=newValue; 

的DataGridView自動刷新(INotifyPropertyChanged的),但它不會觸發事件OnCellValueChanged 。

任何想法如何從我的DataGridView獲取這樣的信息? 它必須在DataGridView級別完成,因爲我正在編寫自己的擴展dgv的控件。

感謝您的幫助。

回答

0

這個我能想到的解決這個問題的方法是使用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()); 
    } 
} 

這樣做的問題是,有沒有辦法(我能想到的),以獲得正確的單元格屬性,所以你將不得不重新評估所有列,而不僅僅是包含更改的列。

相關問題