I have an issue with a combo box control. Items are retrieved from the database, but it won't let me access it via SelectedValue property.
I tried setting it up like this:
DataSet ds = retrieveData(); //I am calling a procedure, it works fine
myComboBox.DataSource = ds;
myComboBox.DisplayMember = "COLUMN1";
myComboBox.ValueMember= "COLUMN2";
But it wouldn't work. The text in the combo box was
System.Data.DataViewManagerListItemTypeDescriptor
So I did this:
foreach (DataRow dr in ds.Tables[0].Rows)
{
myComboBox.Items.Add(
new { TEXT = dr["COLUMN1"].ToString(),
VALUE = Convert.ToInt32(dr["COLUMN2"].ToString())
});
}
Now it works. But I have to access the index (myComboBox.IndexOf("Text inside")
) instead of the value (which is primary key, therefore guaranteed to be unique). SelectedValue is always null, and the SelectedIndex is an anonymous object which fields I can't access either!
Any help?
You should bind your ComboBox to a DataTable
Object rather than a DataSet
.
object. That will solve your purpose. Also, ensure that DropDownStyle
property is Set to DropDownList
. (This way user is forced to select a value from the list rather than typing.)
myComboBox.DataSource = ds.Tables[0];