Roshan
Roshan

Reputation: 3384

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

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

Upvotes: 62

Views: 294171

Answers (6)

rrSep
rrSep

Reputation: 1

This also works: CombBoxItem comboBoxItem = comboBox.SelectedItem as ComboBoxItem string text = comboBoxItem.content.ToString();

Upvotes: 0

Omer
Omer

Reputation: 10174

2023 Update...

You can use as below:

string selected = comboBox.selectedItem.ToString();

or

string selected = comboBox.SelectedText;

Upvotes: 19

Benny Cruger
Benny Cruger

Reputation: 1

var selected = cmbbox.GetItemText(cmbbox.Text);
MessageBox.Show(selected);

Upvotes: 0

vinod Jacob
vinod Jacob

Reputation: 9

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

Upvotes: -3

user1968030
user1968030

Reputation:

Test this

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

Upvotes: 10

Leo Chapiro
Leo Chapiro

Reputation: 13984

Try this:

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

Upvotes: 102

Related Questions