【问题标题】:How to get the value of a selected item in a combobox in a datagridview如何在datagridview的组合框中获取所选项目的值
【发布时间】:2017-04-28 12:58:58
【问题描述】:
我有一个包含供应商列表的组合框供应商。选择供应商后,名为 itemName 的组合框中的值会更改以列出该供应商提供的所有项目。 itemName 组合框位于 datagridview 中。我已经完成了所有这些工作。
现在,我想做的是当在 itemName 组合框中选择一个项目时,我想用该项目的单价更新 datagridview 中的另一列 Unit Price。我想不通的是,如何获取 itemName 组合中所选项目的值?我知道当它在 datagridview 中时,它不像普通的组合框。
【问题讨论】:
标签:
c#
datagridview
combobox
【解决方案1】:
我猜这可能是您正在寻找的。假设您知道组合框的列,您可以使用以下方法获取 DataGridViewComboBoxCell:
DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[RowIndex].Cells[ColumnIndex];
上面的行和列索引是您需要提供的。拥有 ComboBoxCell 后,您可以通过以下方式获取其值或项目索引:
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?");
}
希望这会有所帮助。