我创建了一个 toolStripComboBox
并从数据库中检索所有项目列表选择,如下所示:
private void toolStripComboBox1_Click(object sender, EventArgs e)
{
toolStripComboBox1.ComboBox.ValueMember = "month";
toolStripComboBox1.ComboBox.DataSource = dbConnect.selectMonth(); //get all month from the database
}
组合框然后显示数据库中的所有月份。
后来我尝试使用 selectedItem
从组合框中获取选择,如下所示:
string monthSelect = toolStripComboBox1.SelectedItem.ToString();
但是我得到了值monthSelect = "System.Data.DataRowView"
知道如何获取值而不是System.Data.DataRowView
吗?
最佳答案
找到了解决方案。当使用 toolStripComboBox 的数据源时,如下所示:
toolStripComboBox1.ComboBox.ValueMember = "valueMember";
toolStripComboBox1.ComboBox.DataSource = datasource(); //retrieve value from database into comboBox list
toolStripComboBox1.SelectedItem 将仅返回 DataRow 的自定义 View 。为了获取当前选择的值需要使用:
toolStripComboBox1.ComboBox.SelectedValue.ToString();
关于c# - 从 toolStripComboBox 检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24115921/