1

I am using VS2005 (c#.net desktop application). When I add an eventHandler to a combo of datagridview, it's automatically adding the same eventhandler to all other combos of the 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 different event handlers to different combos in datagridview. Please tell me how can I do it.

numaroth
  • 1,295
  • 4
  • 25
  • 36
Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178

1 Answers1

0

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
  • 3,681
  • 3
  • 27
  • 36