1
Answer

How do I know which combobox is selected?

Remco van Heck

Remco van Heck

14y
15.9k
1

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)