Ren
Ren

Reputation: 775

Retrieve value from toolStripComboBox

I have created a toolStripComboBox and retrieve all the item list selection from the database something like this:

private void toolStripComboBox1_Click(object sender, EventArgs e)
    {
        toolStripComboBox1.ComboBox.ValueMember = "month";
        toolStripComboBox1.ComboBox.DataSource = dbConnect.selectMonth(); //get all month from the database
    }

The comboBox then display all month from database.

Later then I'm trying to fetch the selection from the comboBox using selectedItem somthing like this:

string monthSelect = toolStripComboBox1.SelectedItem.ToString();

However I get the value monthSelect = "System.Data.DataRowView"

Any idea how to get the value instead of System.Data.DataRowView?

Upvotes: 2

Views: 4604

Answers (1)

Ren
Ren

Reputation: 775

Got a solution for this. When using Datasource for toolStripComboBox like :

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

toolStripComboBox1.SelectedItem will only return the customized view of a DataRow. In order to get the value of the current selection need to use :

toolStripComboBox1.ComboBox.SelectedValue.ToString();

Upvotes: 6

Related Questions