badgerbadger
badgerbadger

Reputation: 323

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.

Upvotes: 0

Views: 3287

Answers (1)

JohnG
JohnG

Reputation: 9469

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.

Upvotes: 0

Related Questions