如何在DataGridView中更改单元格值时进行监视?

问题描述:

这是一个WinForms C#问题。如何在DataGridView中更改单元格值时进行监视?

我有一个自定义的DataGridView控件继承自标准的DataGridView类。每当一个单元格添加到网格中时,我想要监视该单元格中的单元格值。我不知道如何做到这一点。

DataBindingCompleted事件在单元格/行/列级别无可奈何。 CellValueChanged事件让自己感到困惑,因为它仅在用户从UI修改值时触发,并且如果该值是从基础数据源更新的,则无法使用。什么是适当的事件来听?

我知道DataGridViewCell类有一个ValueChanging事件。但是在定制的DataGridView中,我怎样才能将我的事件侦听器挂接到每个单元格?

感谢您的帮助。

在您的自定义控制,你需要一个全球性的事件变量:

public event EventHandler CustomCellValueChanged; 

你需要设置更改事件与此单元格:在您的形式

private void gvGridView_CellValueChanged(object sender, EventArgs e) 
    { 
     EventHandler Handler = CustomCellValueChanged; 
     if (Handler != null) { Handler(this, e); }; 
    } 

然后,你就可以检查事件CustomCellValueChanged

+1

这并不能解决原来的问题。它创建了一个无用的CellValueChanged的冗余副本,因为该子类仍然可以访问原始事件。作为指定问题的作者,单元格添加到网格时它不会触发。 – 2010-08-27 18:29:20

+0

同意布拉德利。但是,仍然感谢你的努力。 – Steve 2010-08-28 00:56:19

+0

对不起,看错了这个问题。我的错。 – Wildhorn 2010-08-30 12:16:23

1,您可以在定制DataGridView时继承DataGridView。如果继承UserControl来定制DataGridView,则无法直接获取CellValueC在其他项目或应用程序中生成自定义DataGridView时挂起的事件。

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; .The句子是在该方法的InitializeComponent()。