How to monitor whenever a cell value is changed in DataGridView?
This is a WinForms C# ques开发者_高级运维tion.
I have a customized DataGridView control inherited from the standard DataGridView class. I want to monitor the case whenever a cell is added to the grid, a cell value is changed in the grid. I have no idea how to do this.
The DataBindingCompleted event is helpless in cell/row/column level. The CellValueChanged event is confusing itself since it is only fired when user modifies a value from UI and is helpless if the value is updated from underlying data source. What is the proper event to listen to?
I know DataGridViewCell class has a ValueChanging event. But in the customized DataGridView, how can I hook my event listener to every cell?
Thanks for the help.
1、You may inherit DataGridView when customized DataGridView.If inherit UserControl to customize DataGridView,you can not directly get CellValueChanged event when generate customized DataGridView in other project or application.
2、To do something in the CellValueChanged.
3、Inherit DataGridView implement.
(1) Create UserControl.Name is DataGridViewEx.
(2) Modify inherit.
public partial class DataGridViewEx : UserControl
==>public partial class DataGridViewEx :DataGridView
(3)Open DataGridViewEx.Designer.cs and shield //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
.The sentence is in the method InitializeComponent().
In your custom control, you need a global event variable:
public event EventHandler CustomCellValueChanged;
you need to set the cell changed event with this:
private void gvGridView_CellValueChanged(object sender, EventArgs e)
{
EventHandler Handler = CustomCellValueChanged;
if (Handler != null) { Handler(this, e); };
}
Then in your form, you will be able to check the event CustomCellValueChanged
精彩评论