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.

11 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

  10. sachin kalse Says:

    Hi,
    Thanks for the “life saving” work around.
    I am using it further to enable a button which is outside of grid depending
    on the value selected in ComboBoxCell.
    The code below works well if I change the values of only one ComboBox, but
    when I try to change values in multiple it fails.
    e.g. to disable the Save button I have to make all ComboBox values = “Select”
    which is default value, even after that IsItemSelected method returns “true”

    After inserting a break point at
    >> if (cell.Value.ToString() != “Select”)
    I still get the last value in Cell which is other than “Select”

    void comboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
    int selectedIndex = ((ComboBox)sender).SelectedIndex;
    //MessageBox.Show(“Selected Index = ” + selectedIndex);
    if (selectedIndex > 0)
    {
    btnSave.Enabled = true;
    }
    else
    {
    // check for all rows
    btnSave.Enabled = (IsItemSelected() == true ? true : false);
    }
    }

    private bool IsItemSelected()
    {
    bool isItemSelected = false;
    foreach (DataGridViewRow dgvRows in dgvPending.Rows)
    {
    DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dgvRows.Cells[0];
    if (cell != null)
    {
    if (cell.Value.ToString() != “Select”)
    {
    isItemSelected = true;
    break;
    }
    }
    }
    return isItemSelected;
    }

    Please guide
    Thanks in advance


Leave a Reply