Remco van Heck

Remco van Heck

  • 1.7k
  • 53
  • 42.6k

How do I know which combobox is selected?

Oct 21 2010 8:40 AM

I have a windowsform with a datagridview. The datagridview has 3 comboboxes.

When I select a combobox and change the value there must something happen.

The problem is that I don't know how I can tell whicht combobox is selected.

I have the followingcode:

 private void NaardataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

   //here we will add the combo box's selected event changed ComboBox cmbBox;

  if (NaardataGridView2.CurrentCell.ColumnIndex == 0)

  {

        cmbBox = e.Control as ComboBox;

         if (cmbBox == null) { return; }

        cmbBox.SelectedIndexChanged += cmbBox_SelectedIndexChanged;

  }

  }

void cmbBox_SelectedIndexChanged(object sender, EventArgs e)

{

int rowNr = NaardataGridView2.CurrentRow.Index;

ComboBox cmbBox = (ComboBox)sender;

NaardataGridView2.Rows[rowNr].Cells[4].Value = Convert.ToString(cmbBox.SelectedIndex);

}


before it does --> NaardataGridView2.Rows[rowNr].Cells[4].Value = Convert.ToString(cmbBox.SelectedIndex);

I want to know which combobox is selected, how can I do this?

This code doesn't work because the name is empty--> if (Convert.ToString(cmbBox.Name) == "ab") {}

Does some one know what I do wrong?


Answers (1)