Converting a record in a combolist to string

rbharris

Member
Joined
May 26, 2006
Messages
14
Programming Experience
Beginner
I've populated a combolist with a list of records from a database. I need to be able to use the items in the combolist as string items to pass to a sql string.

When the combolist.selectedItem fires I get this:
Conversion from type 'DataRowView' to type 'String' is not valid.

I (somewhat) understand the problem here; I just need to know if it is possible to convert this to string and how.

Thanks.
 
You don't convert the DataRowView to a String because generally it will contain values from a number of columns. If you're binding data to a ComboBox then normally you would assign the DataTable to the DataSource property, the name of the column whose data you want to display to the DisplayMember property and the name of the column whose data you want to use to the ValueMember property. When the user makes a selection you then get the value from the ValueMember column from the SelectedValue. Note that you can assign the same column to the DisplayMember and ValueMember properties. If you are using the ValueMember for one column and you still want to get the actual string displayed in the ComboBox you could use the Text property or else use myComboBox.GetItemText(myComboBox.SelectedItem). Note that you can also use the GetItemText method to get the string displayed for items other than the SelectedItem.
 
Back
Top