Datagridview: Get SelectedIndex of DataGridViewComboBoxColumn

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.

26 thoughts on “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

  11. sree says:

    thanks a lot. I am using a DataGridViewComboBoxCell. Now with ur solution i am able to get the selectedindex but can i delete this control(comboboxcell) and get a textboxcell back on to the cell.

  12. Asif says:

    Nice one…

    it really helps me….

  13. fausto says:

    Just Beautiful

  14. shantala says:

    Good one… Thanks a lot

  15. Suresh Londhe. says:

    OOhhhhhhhh……

    What A Solution!!!!!

    Realy Helps Lottt…
    Thanks Lottt…

  16. Pushparaj says:

    It is really help me move next step of my project. Thanks a lot and keep up good work. Thanks a lot.

  17. Zolfaghari says:

    Thanks a lot.
    God bless you.

  18. sachin surve says:

    Dear Sir,

    I am using Datagridview in my new billing project where in i am opting DatagridviewComboboxColoumn to populate the items ( e.g. Packing Charges, Discount, Sales Tax,Freight etc).

    I am able to populate the items in combobox but are not able to get seleceted value of combobox event and fill up the other DatagridviewTextboxes on that selected value.

    Can anybody help me please.

  19. Amit Jha says:

    Dear sir
    your code add new combobox with new data but i have a question i want to get selected index of existing datagridveiwcombobox in datagridview …if you have any idea?

    pls help me!!!!!!!!!

  20. EM says:

    Awesome. Thanks!!

  21. bsom says:

    Work fine for me with one change. I had to remove event handler after invoke event like this:

    12 void comboBox_SelectedIndexChanged(object sender, EventArgs e)
    13 {
    14 int selectedIndex = ((ComboBox)sender).SelectedIndex;
    15 MessageBox.Show(“Selected Index = ” + selectedIndex);

    comboBox.SelectedIndexChanged -= new EventHandler(comboBox_SelectedIndexChanged);

    16 }

  22. bsom says:

    Sorry mistake , should be:

    ((ComboBox)sender).SelectedIndexChanged-=EventHandler(comboBox_SelectedIndexChanged);

  23. Awesome:)

    I had this code:

    And in the code behind I added:

    private void DocTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    var item = documentFilesDataGrid.SelectedItem;

    if (item != null)
    {
    int selectedIndex = ((ComboBox)sender).SelectedIndex;
    currentSession.fileList[0].Doc_Type = docTypes[selectedIndex].ToString();
    DebugLabel.Content = selectedIndex;
    }

    }

    Works like charm!:D:D Awesome!

  24. Moon shine says:

    thanks a lot ,It’s so useful

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.