c# - 从 toolStripComboBox 检索值

标签 c# toolstripcombobox

我创建了一个 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/

相关文章:

.net - ToolStripComboBox -- 自动调整项目大小

c# - 防止单选按钮点击

c# - Windows 8 Developer Preview 中缺少 Type.GetProperty() 方法

c# - 模型未在 ASP.NET MVC 3 中传递

c# - WPF Caliburn Micro Message.Attach 未通过 HierarchicalDataTemplate 冒泡

c# - 系统核心错误 : "Code supposed to be unreachable" using C# Entity Framework and Linq with Expression

c# - 如何制作 ToolStripComboBox 来填充 ToolStrip 上的所有可用空间?