I have a problem with my combobox. I've set DataSource which contains a list of objects, DisplayMember and also ValueMember but there are times when the combobox displays the namespace where the object class is placed. For example: Project.Interface.Object

cmbAuto.DataSource = Collections.ProfileList.FindAll(t => t.IsAuto);
cmbAuto.DisplayMember = "Name";
cmbAuto.ValueMember = "ID";
cmbAuto.SelectedIndex = -1;

Can you please give me an advice in order to solve this?
Thank you

Dani AI

Generated

What you saw (the control showing something like a namespace + class) happens when the binder cannot use the member name you supplied and the control ends up using the object's string representation. pointed in that direction, and confirmed that changing the object output removed the symptom — but the underlying causes are worth checking so the binding works reliably.

Try this quick checklist:

  • Verify DisplayMember names an actual public property (has a public getter). A public field will not work the same way as a property.
  • Make sure every item in the collection exposes that property (no nulls, no mixed types that lack the member).
  • Avoid anonymous/internal types across assembly boundaries for binding; use a concrete POCO with public properties.
  • Prefer setting DisplayMember/ValueMember before assigning DataSource, or bind via a BindingSource to avoid timing/order issues.
  • After changes, call ResetBindings on your BindingSource (or refresh the control) to force the UI to re-evaluate.

A common, robust pattern is to use a BindingSource so the control always has a consistent, typed source:

var bs = new BindingSource();
bs.DataSource = profiles.Where(p => p.IsAuto).ToList();
comboBox.DisplayMember = "Name";
comboBox.ValueMember = "ID";
comboBox.DataSource = bs;
comboBox.SelectedIndex = -1;

Overriding ToString() (what did) is a practical fallback if you want a quick display value, but it masks the binding mismatch rather than fixing it. For authoritative details on how DisplayMember is used and best practices for data binding, see the official docs: ListControl.DisplayMember and BindingSource.

Recommended Answers

All 2 Replies

Its caused by passing an object to the datasource and not an actual value within the object.

Ie.

TextBox txtExample = new TextBox();
Console.WriteLine(txtExample.ToString()); //Outputs the literal textbox class to a string
Console.WriteLine(txtExample.Text); //Outputs the text field value

You need to ensure your not passing a class into the DG.

Thank you a lot. I've seen that if I override the class, then the problem disappears.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.