0

I'm building a Winforms application which has a DataGridView. The DataGridView is not bound to a datasource. I have a comboboxColumn on my grid which I'm populating using a datatable.

When I try to retrieve the selectedValue of the comboBoxCell, it is giving the first matched value instead of the exact selected value.

Convert.ToString((datagridview1.Rows[i].Cells["columnName"] as DataGridViewComboBoxCell).Value)

For Example, The combobox datatable is

DisplayMember ValueMember
Orange        1111
Apple         2222
Banana        3333
Apple         4444
Guava         5555

Now, if I select the Apple with ID 4444, the above code gets the Apple with 2222.

I tried to perform the steps given in below link, but that is giving the index instead of the value.

https://stackoverflow.com/a/30157754/3619679

Community
  • 1
  • 1
dsouzaleo
  • 86
  • 8
  • What if you just call `DataGridView1.Rows[i].Cells["columnName"].Value`.But Your code look also good. Just for test. Im always using as i wrote. Maybe in this case it will be working. And be sure you put `ValueMember`. – M. Wiśnicki Dec 15 '16 at 19:23
  • Yes, I tried that. But, if there are duplicate values with different ValueMember IDs, It gives the first value in the comboxlist instead of the actual choosen value. – dsouzaleo Dec 16 '16 at 05:31

1 Answers1

0

I was able to resolve the issue with help from the below link

https://stackoverflow.com/a/30157754/3619679

SelectedItem stores the correct value of the selected comboBox Item.

private void datagridview1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (cmbCombo != null)
            {
                DataRowView oDataRowView = cmbCombo.SelectedItem as DataRowView;
                string sValue = string.Empty;

                if (oDataRowView != null)
                {
                    sValue = oDataRowView.Row["ValueMemberID"] as string;
                }
                datagridview1[e.ColumnIndex, e.RowIndex].Tag = sValue;
            }

        }
Community
  • 1
  • 1
dsouzaleo
  • 86
  • 8