C# Datagridview: get selected item in combobox columns

50,815

Solution 1

The Control in a DataGridView is not a ComboBox, it is a DataGridViewComboBox and has different properties and methods. From MSDN

Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.

However, you mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article (How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List).

You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.

Solution 2

If we have bound a datagridcomboboxcell with a different DisplayMember and ValueMember, like so:

dgcombocell.DisplayMember = "Name"; 
dgcombocell.ValueMember = "Id";  
dgcombocell.DataSource = dataset1.Tables[0];

Then for getting SelectedText, and SelectedValue, we can write this code:

string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);

I hope it solves your problem.

Solution 3

Use this to get or set selected value:

object selectedValue = currentRow.Cells["comboboxColumnName"].Value

Don't forget to set DisplayMember and ValueMember for your DataGridViewComboBoxColumn

Solution 4

This is how it is done

  DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dgv.Rows[0].Cells[1];

  MessageBox.Show(""+comboCell.Items.IndexOf(comboCell.Value));
Share:
50,815
tf.rz
Author by

tf.rz

:)

Updated on July 09, 2022

Comments

  • tf.rz
    tf.rz almost 2 years

    I'm working on a GUI that allows the user to manipulate xml files. I display the xml file in a datagridview organized neatly by columns through xml elements. I allow the user to add columns as an extention on my project. The column gets added to the dataset table, then updated to the datagridveiew that I use to display the xml file in. I've included the ability for the user to add a combobox column to select choices instead of entering them in constantly like.. true or false. However, that is where the problem lies. Saving a normal column was easy. The combobox column is being a pain.

    I have a "save combobox column" to have it updated to the xml and a "save" button to save in a destination of the user's choice.

    I've done some research and it seems like the combobox class has such a feature to gain access to the selecteditem in the combobox put in by the user. Where we have:

        ComboBox box = new ComboBox();
        box.SelectedItem;
    

    I tried applying this to the combobox column class but it does not have such a function. Thus, I cannot figure out how to directly obtain the value of the user's selected item. I tried experimenting with comboboxcell's as well, but that didn't lead me anywhere either. Both those classes I played around with do not have a... "selected item" function and even google does not have a solution for me. =( I've also tried using the cell.value, but it is "null" for some reason. Even when the user selects an item in the box, it doesn't get saved into the cell's value.

    TLDR: My question in short is, how, if possible, do you gain access to the comboboxcolumn cell's selected item? Additionally, how would you then ensure that the item value is saved in the cell?

    Thanks in advance. I'm using .NET 3.5 SP1, through Visual Studio 2008 C#.

    Sincerely,

    tf.rz