views:

24

answers:

1

i am using VS2005 (c#.net desktop application). when i am adding eventHandler to a combo of datagridview, its automatically adding same eventhandler to all other combos of same datagridview..

my code:

private void dgvtstestdetail_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {

        DataGridView grid = (sender as DataGridView);



        if (grid.CurrentCell.OwningColumn == grid.Columns["gdvtstd_TestParameter"])
        {

            ComboBox cb = (e.Control as ComboBox);
            cb.SelectedIndexChanged -= new EventHandler(dvgCombo_SelectedIndexChanged);
            cb.SelectedIndexChanged += new EventHandler(dvgCombo_SelectedIndexChanged);

        }
 }

i want to add differnt event handelrs to differnt combos in datagridview. please tell me how can i do it..

A: 

By default, I believe that the DataGridColumn reuses the same instance of ComboBox for each cell. This is an optimization used by the grid to keep the number of created editing controls low.

The easiest thing is just to have one event handler, check the cell being edited, and take the appropriate action.

public void dvgCombo_SelectedIndexedChanged()
{
    if (<condition1>)
        ExecuteConditionOneLogic();

    if (<condition2>)
        ExecuteConditionTwoLogic();

}

A more advanced solution would be to create your on custom DataGridViewColumn implementation which does not share an editing control. I wouldn't recommend this unless you really have some reusable functionality.

Chris McKenzie