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.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.
string strID = MyCombobox2. SelectedValue. ToString();
You can use the SelectedText property to retrieve or change the currently selected text in a ComboBox control. However, you should be aware that the selection can change automatically because of user interaction.
However, if you want to get the index of selected items in the combobox widget, then you can use the get() method. The get() method returns an integer of the selected item known as the index of the item.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With