简体   繁体   中英

How to get the value of a selected item in a combobox in a datagridview

I have a combobox Supplier with a list of Suppliers. When a supplier is selected, the values in a combobox called itemName change to list all the items supplied by that supplier. The itemName combobox is in a datagridview. I have all this working to that point.

Now, what I want to do is when an item is selected in the itemName combobox, I want to update another column Unit Price in the datagridview with the unit price of that item. What I can't figure out, is how do I get the value of the selected item in the itemName combo? I know when it's in a datagridview, it's not like a normal combobox.

I am guessing this may be what you are looking for. Assuming you know the column of your comboBox, you can grab the DataGridViewComboBoxCell by using:

DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[RowIndex].Cells[ColumnIndex];

Above the row and column indexs are what you need to supply. Once you have the ComboBoxCell then you can get its value or items index with:

if (cb.Value != null)
{
  // do stuff
  MessageBox.Show(" Index of current comboBox items is: " + cb.Items.IndexOf(cb.Value) + " current displayed value: " + cb.Value);
}
else
{
   MessageBox.Show("Combo Box is empty?");
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM