2014-06-09 25 views
2
檢索值

我創建了一個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財產以後這樣從ComboBox選擇:

string monthSelect = toolStripComboBox1.SelectedItem.ToString(); 

但是我得到的值monthSelect = "System.Data.DataRowView"

任何想法如何獲得價值,而不是System.Data.DataRowView

回答

3

爲此得到了一個解決方案。當使用數據源的ToolStripComboBox控件,如:

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

toolStripComboBox1.SelectedItem只會返回一個DataRow的自定義視圖。爲了得到當前選擇的值需要使用:

toolStripComboBox1.ComboBox.SelectedValue.ToString();