c# – ComboBox.ValueMember problem during subelements creation – Education Career Blog

I have some problem with my combobox, I’m trying to fill it with some values from database, everything work fine, except the fact that last line of this code (with ValueMember in it) generate UnknownException in free translation it says that it cannot create a list of sub elements for the “table” field.

connection = new MySqlConnection(connectionString);
connection.Open();

MySqlDataAdapter mda = new MySqlDataAdapter(query, connection);

DataSet my_dataset = new DataSet("dataset");
mda.Fill(my_dataset);
connection.Close();

DataViewManager dvm_cb2 = my_dataset.DefaultViewManager;
my_combobox.DataSource = dvm_cb2;
my_combobox.DisplayMember = table + "." + name;
my_combobox.ValueMember = table + "." + my_id;

Any ideas, what could be the problem?

,

DisplayMember and ValueMember should be string repreentations of the object property

So for example if you had a collection of the following object

public class MyObject
{
    public string ItemId = "";
    public string DisplayValue = "";
}

So you make a collection of the object

List<MyObject> list = new List<MyObject>();

Then assign this and the properties to the combo box

combobox.DataSource = list ;
combobox.DisplayMember = "DisplayValue";
combobox.ValueMember = "ItemId ";

That should work.

,

try out this

my_combobox.DisplayMember = "fieldname";
my_combobox.ValueMember = "fieldname";
my_combobox.DataSource = dvm_cb2;

Leave a Comment