locked
DataGridViewComboBoxCell: How do I find out SelectedIndex? RRS feed

  • Question

  • I know this question has been answered, but none of the solutions seem to work for me.

    I have a DataGridViewComboBoxColumn bound to a data source. I want to get the selected index (not the value) of the cell.

    I tried this:

    Code Block

    DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell) dgv[columnIndex, rowIndex];
    int index = cell.Items.IndexOf(cell.Value.ToString());



    index is -1. It's funny why it's -1, I am using the value of the cell, so the value can't be 'wrong'.

    So, how do I get the SelectedIndex?
    Wednesday, November 28, 2007 4:56 AM

Answers

  • Hi, Andrews,

     

    Based on my understanding, you cannot get the SelectedIndex of a DataGridViewComboBoxCell, can you?

     

    I think the causation of this issue is that you bind a DataSource to your DataGridViewComboBoxColumn.

    Please read this article.

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcell.items.aspx

    And it says

    "If the DataSource property is set, then the Items property cannot be used."

     

    In fact, when the DataGridViewComboBoxColumn is using DataBinding, the Items property is actually a List of DataRowView, and it cannot be compared with the cell values.

     

    I have a workaround for you, you can compare the value with your DataSource.

    For example.

    Code Block

            DataTable selection;

            private void Form1_Load(object sender, EventArgs e)

            {

                selection = new DataTable();

                selection.Columns.Add("options", typeof(string));

                selection.Rows.Add("abc");

                selection.Rows.Add("bcd");

                selection.Rows.Add("efg");

     

                DataTable datatable = new DataTable();

                datatable.Columns.Add("data", typeof(string));

                datatable.Rows.Add("abc");

                datatable.Rows.Add("efg");

                datatable.Rows.Add("abc");

     

                DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();

                column.DisplayMember = "options";

                column.ValueMember = "options";

                column.DataPropertyName = "data";

                column.DataSource = selection;

     

                dataGridView1.Columns.Add(column);

     

                dataGridView1.DataSource = datatable;

                dataGridView1.CellMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseClick);

            }

     

            void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)

            {

                DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];

                int index = compare(selection, cell.Value);

                Console.WriteLine(index.ToString());

            }

     

            private int compare(DataTable source, object value) //Compare with the value in the DataSource

            {

                int result=-1;

                for (int i = 0; i < source.Rows.Count; i++)

                {

                    if (source.Rows[i][0] == value)

                    {

                        result = i;

                        break;

                    }

                }

                return result;

            }

     

     

    Hope this helps,

    Regards

     

     

     

     

     

    Monday, December 3, 2007 3:12 AM