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

时间:2016-08-11 09:11:08

标签: c# .net loops for-loop

我在使用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。

那么有没有机会使用CellValueChanged 在这个以编程方式添加的DataGridView中,或者如果它不可能在DataTable上有机会吗?

任何人都知道实现这个目标的好方法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用<?php mkdir("folder", 777, true); ?> 运算符以编程方式添加事件处理程序,如下所示:

+=

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

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);