从 toolStripComboBox 检索值

Ren*_*Ren 2 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
    }
Run Code Online (Sandbox Code Playgroud)

然后组合框显示数据库中的所有月份。

后来我尝试使用selectedItem类似这样的东西从组合框中获取选择:

string monthSelect = toolStripComboBox1.SelectedItem.ToString();
Run Code Online (Sandbox Code Playgroud)

不过我得到了价值monthSelect = "System.Data.DataRowView"

知道如何获取值而不是吗System.Data.DataRowView

Ren*_*Ren 5

对此有一个解决方案。当使用 toolStripComboBox 的数据源时,例如:

toolStripComboBox1.ComboBox.ValueMember = "valueMember";  
toolStripComboBox1.ComboBox.DataSource = datasource();  //retrieve value from database into comboBox list
Run Code Online (Sandbox Code Playgroud)

toolStripComboBox1.SelectedItem 将仅返回 DataRow 的自定义视图。为了获取当前选择的值需要使用:

toolStripComboBox1.ComboBox.SelectedValue.ToString();
Run Code Online (Sandbox Code Playgroud)