Display object propery in ComboBox

c#, winforms

Solution

With regards to my comment, it might be needed to set the `DisplayMember` and `ValueMember` properties of the ComboBox, like so;

cmbRoles.DisplayMember = "Role";
cmbRoles.ValueMember = "Id";
cmbRoles.DataSource = avail;

This way your ComboBox will Display the role, but the underlying data will be the ID, So when you select via the `SelectedValue` property, you'll get the ID.

Problem

I have a custom object which holds details about a project resource. Properties are PersonName, Position and Id If the resource isn't filled, PersonName is set to 'Unassgined'. To add an object to a Combobox, I do: ``` var avail = s.GetUnassignedPrintRoles(SprintId); foreach (var o in avail) { cmbRoles.Items.Add(o); } ``` This is fine when displaying a list of resources. My object has an overridden ToString() method: ``` public override string ToString() { if(AssignedPerson != null) return ResourceType + " - " + AssignedPerson.Firstname + " " + AssignedPerson.Surname; return "Unassigned"; } ``` But, I have a screen that shows a list of roles that are not assigned. So, I get a list, where the Person is NULL. But, I want to display the 'Role' in the ComboxBox. But, my object's ToString shows 'Unassigned'. How can I make it display the Role property? Is there a way to save the object in the Comboxbox item, but display a different propery in the display, other than what I have set in the ToString override?

Original source