繁体   English   中英

在DataTable /上以编程方式添加了DataGridView的CellValueChanged

[英]CellValueChanged on DataTable/ programatically added DataGridView

我在CellValueChanged上遇到了一些问题。

我可以这样做,并且可以正常工作:

    private void dataGridView_etykietyA6_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        //do some stuff
    }

但是我不得不手动添加DataGridView,但是我想通过以下方式添加它们:

        for (int i = 0; i < lista.Count; i++)
        {
            tabControl_Roz.TabPages.Add("Typ "+lista[i]);
            tabControl_Roz.TabPages[i + 1].Controls.Add(new DataGridView()
            {
                Name = "dataGridView_" + lista[i],
                Dock = DockStyle.Fill
            });
        }

DataGridView添加可以正常工作,但我不知道如何在其上使用CellValueChanged。

那么,是否有机会在以编程方式添加的DataGridView上使用CellValueChanged;或者如果不可能,是否有可能在DataTable上使用CellValueChanged?

有人知道实现此目标的好方法吗?

您可以使用+=运算符以编程方式添加事件处理程序,如下所示:

var dataGridView = new DataGridView()
{
    Name = "dataGridView_" + lista[i],
    Dock = DockStyle.Fill
};
dataGridView.CellValueChanged += new EventHandler(dataGridView_etykietyA6_CellValueChanged); // this is the name of your handler method
tabControl_Roz.TabPages[i + 1].Controls.Add(dataGridView);

或者您甚至可以像这样内联编写处理程序逻辑:

dataGridView.CellValueChanged += (e, sender) => {
// your handler logic
};

暂无
暂无

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

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