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

C#Combobox

C# Problem Overview


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

C# Solutions


Solution 1 - C#

Try this:

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

Solution 2 - C#

You can use as below:

string selected = cmbbox.Text;
MessageBox.Show(selected);

Solution 3 - C#

Test this

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

 

Solution 4 - C#

SelectedText = this.combobox.SelectionBoxItem.ToString();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRoshanView Question on Stackoverflow
Solution 1 - C#Leo ChapiroView Answer on Stackoverflow
Solution 2 - C#OmerView Answer on Stackoverflow
Solution 3 - C#user1968030View Answer on Stackoverflow
Solution 4 - C#vinod JacobView Answer on Stackoverflow