JustSomeNewbie
JustSomeNewbie

Reputation: 115

CellValueChanged on DataTable/ programatically added DataGridView

I'm having some problem with CellValueChanged.

I can do it this way and it works :

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

But I had to add my DataGridView manually then, but I want to add them by :

        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 adding works correctly but I don't know how to use CellValueChanged on it.

So is there any chance to use CellValueChanged on this programatically added DataGridView or if it's not possible is there any chance to do it on DataTable?

Anyone know good way to accomplish this?

Upvotes: 0

Views: 146

Answers (1)

ttaaoossuu
ttaaoossuu

Reputation: 7884

You can add event handlers programmatically with += operator like this:

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

or you can even write your handler logic inline like this:

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

Upvotes: 1

Related Questions