Reputation: 295
ComboBox is binding database
string str= comboBox1.SelectedItem.ToString();
the line gives System.Data.DataRowView
value for str
but not giving selected item name.
Upvotes: 2
Views: 5724
Reputation: 2338
Use the DisplayMember
and ValueMember
Properties for the combobox before you assign the DataSource
, and use SelectedValue
instead of SelectedItem
.
For example, if you have a List<MyClass>
- where MyClass
has a property int ID
, and another one string Title
- and you want to assign it as the DataSource
of comboBox1
, you should write:
List<MyClass> myList;
...
comboBox1.DisplayMember = "Title";
comboBox1.ValueMember = "ID";
comboBox1.DataSource = myList;
Now comboBox1.SelectedValue
is an object{int}
, which can be casted to int
and used.
Upvotes: 2
Reputation: 4683
try this
if (comboBox1.SelectedItem is DataRowView) {
string sval = ((DataRowView)comboBox1.SelectedItem).Row["ColumnName"].ToString();
}
Upvotes: 4
Reputation: 384
If you want the text of the selected item, just use comboBox1.Text
.
Upvotes: 2
Reputation: 10267
ToString() is inherited from the Object-Class. The default implementation states the Class Name of the corresponding object.
you may want to cast the SelectedItem to a DataRowView to access the column-values for that row
Ex:
String str = ((DataRowView)comboBox1.SelectedItem)["ColumnNameHere"];
Upvotes: 1