How to get the selected item of a combo box to a string variable in c#

Roshan picture Roshan · Mar 3, 2013 · Viewed 205k times · Source

Can anyone tell me how to get the selected item of a ComboBox to a string variable?

string selected = cmbbox.SelectedItem.ToString();
MessageBox.Show(selected);

This gives me System.Data.DataRowView in my MessageBox

Answer

Leo Chapiro picture Leo Chapiro · Mar 3, 2013

Try this:

string selected = this.ComboBox.GetItemText(this.ComboBox.SelectedItem);
MessageBox.Show(selected);