I have a BindingList with my class where I would like to populate a ComboBox using a property of it so when my list changes the ComboBox would change as well.
public class UserAccess
{
public override string ToString()
{
return Access;
}
public int AccessId { get; set; }
public string Access { get; set; }
public List<string> Command = new List<string>();
public bool HasCommand(string cmd)
{
return this.Command.Any(x => x == cmd);
}
}
public BindingList<UserAccess> accessList = new BindingList<UserAccess>();
On my form load I assign it to the ComboBox:
myComboBox.DataSource = accessList;
I want to populate the box with Access or with the AccessId as value and Access as the printed name.
Problem is that it will print only the last item of the list to the combobox what am I doing wrong ?
Use
DisplayMemberto specify what field to use for display in theComboBox.Make
accessListreadonlyto guarantee that you never recreate a new instance of the list. If you don’t make itreadonly, this may introduce a subtle bug, if you don’t reassignDataSourcewhenever you recereateaccessList.If you need to be able to change items properties in accessList (like
accessList[0].Access = "Test3") and see the changes reflected in UI, you need to implementINotifyPropertyChanged.For example: