[英]How to monitor whenever a cell value is changed in DataGridView?
這是一個WinForms C#問題。
我有一個從標准DataGridView類繼承的自定義DataGridView控件。 我想監視每當單元格添加到網格時的情況,在網格中更改單元格值。 我不知道該怎么做。
DataBindingCompleted事件在單元格/行/列級別中無能為力。 CellValueChanged事件本身令人困惑,因為它僅在用戶修改UI中的值時觸發,如果從基礎數據源更新值,則無助。 聽什么是正確的事件?
我知道DataGridViewCell類有一個ValueChanging事件。 但是在自定義的DataGridView中,如何將我的事件監聽器掛鈎到每個單元?
謝謝您的幫助。
1,您可以在自定義DataGridView時繼承DataGridView。如果繼承UserControl來自定義DataGridView,則在其他項目或應用程序中生成自定義DataGridView時無法直接獲取CellValueChanged事件。
2,在CellValueChanged中做一些事情。
3,繼承DataGridView工具。
(1)創建UserControl.Name是DataGridViewEx。
(2)修改繼承。 public partial class DataGridViewEx : UserControl
==> public partial class DataGridViewEx :DataGridView
(3)打開DataGridViewEx.Designer.cs並屏蔽//this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
。句子在InitializeComponent()方法中。
在自定義控件中,您需要一個全局事件變量:
public event EventHandler CustomCellValueChanged;
你需要設置單元格更改事件:
private void gvGridView_CellValueChanged(object sender, EventArgs e)
{
EventHandler Handler = CustomCellValueChanged;
if (Handler != null) { Handler(this, e); };
}
然后在您的表單中,您將能夠檢查事件CustomCellValueChanged
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.