开发者

How to get the data in comboBox C#

I have a combo box and I populated it this way:

DataTable dt = new DataTable();

using (SQLiteConnection conn = connection.Conn)
{               
    using (SQLiteCommand cmd = new SQLiteCommand(conn))
    {
        cmd.CommandText = "select id, description from category";
        conn.Open();

        using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd))
        {
            da.Fill(dtChargeCodes);
            comboBox1开发者_StackOverflow.DataSource = dt;
            comboBox1.DisplayMember = "description";
            comboBox1.ValueMember = "id";
        }                                       
    }               
}

What I'm trying to achive is to get the data of the selected item in the comboBox but when I tried to display it using MessageBox.Show(comboBox1.SelectedItem.ToString()); what I get is the type System.Data.DataRowView. not the actual value of the field description in the table category. Please help... thanks.


Either use

comboBox1.SelectedText

or

((System.Data.DataRowView)(comboBox1.SelectedItem))["description"]

You may need to use the second method if you need to access the value in the SelectedIndexChanged event (see here)


Try this:

MessageBox.Show(comboBox1.SelectedValue);


I think you need

MessageBox.Show(comboBox1.SelectedItem.Value.ToString())


If you are looking for the screen value use this:

MessageBox.Show(combobox1.SelectedText);


Some explanation:

MessageBox.Show(comboBox1.SelectedValue.ToString()); //get selected item value
MessageBox.Show(comboBox1.Text); //get selected item text
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜