Cellclick event in multiple dynamic datagridviews

99 views Asked by At

I have a windows form where data is being fetched from multiple SQL tables. If the required data is present , then it is displayed in dynamically generated DataGridView. I have added a button and a cellclick event to these dynamically generated datagridviews. The issue is that the cellclick event of different datagridviews references to the same datagridview(ex. - here in the below image - if I click "2" then data from "1" is picked up).

enter image description here

The code is as per below

d3 = new DataGridView();
d3.DataSource = ds;
d3.CellClick += datagridview_CellClick;
d3 =addbuttontodatagridview(d3);
addtopanel(d3);

The cell click event is -

private void datagridview_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if(e.ColumnIndex==0)
        {
            Console.WriteLine(d3.Rows[e.RowIndex].Cells[2].Value.ToString()); 
            Console.WriteLine(e.RowIndex);
        }

I understand that since I am assigning all datagridviews to d3, hence it is referencing only to the first one. But I am not sure how can I make dynamic cell click events for multiple dynamically created datagridviews

0

There are 0 answers