Datagridview: Get SelectedIndex of DataGridViewComboBoxColumn

December 7, 2007

This seems to be a straight forward thing, but Microsoft only knows why this simple feature is not supported in DataGridView.

If you have a ComboBoxColumn in your DataGridView and you want to know what is the selected index of the combo box, then you need to do this:

1. Handle the EditingControlShowing event of DataGridView. In this event handler, check if the current column is of our interest. Then we create a temporary ComboBox object and get the selected index:


private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
// Check box column
ComboBox comboBox = e.Control as ComboBox;
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}

void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = ((ComboBox)sender).SelectedIndex;
MessageBox.Show("Selected Index = " + selectedIndex);
}

A rather round-about solution where a simple one would have sufficed.

10 Responses to “Datagridview: Get SelectedIndex of DataGridViewComboBoxColumn”

  1. André Says:

    You can do it using:

    ((DataGridView)sender).CurrentCell.RowIndex

  2. Durga Says:

    Great help

  3. Shashi Bhushan Singh Says:

    Excellent Job!!! . Keep It Up . God Bless u with more nurturing capabilities so that programmers like us keeps on geetting tips from u…………….. Thank u…………………………

  4. E.! Says:

    Nice piece of code! Thanks

  5. Subrata K Says:

    Thanks man,

    It’s exactly what i want.

  6. Unnikrishnan Says:

    Its working thanks

  7. Mathew Abraham Says:

    Thanks a lot , I was struggling for 1 day for this

  8. Anand Says:

    Thanks dude…. This code is helped me lot…

  9. raviranjan Says:

    thank u very much dear …..i got lot of help from this effective code


Leave a Reply