I'm using .NET 2.0 and I'm trying to bind a combobox's Datasource to a sorted dictionary.
So the error I'm getting is "DataMember property 'Key' cannot be found on the Datasource".
SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache(); userListComboBox.DataSource = new BindingSource(userCache, "Key"); //This line is causing the error userListComboBox.DisplayMember = "Key"; userListComboBox.ValueMember = "Value";
Show activity on this post. SortedDictionary<string, int> userCache = new SortedDictionary<string, int> { {"a", 1}, {"b", 2}, {"c", 3} }; comboBox1. DataSource = new BindingSource(userCache, null); comboBox1. DisplayMember = "Key"; comboBox1.
To bind a ComboBox or ListBox control If you are binding to a table, set the DisplayMember property to the name of a column in the data source. If you are binding to an IList, set the display member to a public property of the type in the list.
ComboBox Control C# controls are located in the Toolbox of the development environment. You can use them to create objects on a form with a simple series of mouse clicks and dragging motions. A ComboBox displays a Textbox combined with Listbox, which enables the user to select items from the list or enter a new value.
SortedDictionary<string, int> userCache = new SortedDictionary<string, int> { {"a", 1}, {"b", 2}, {"c", 3} }; comboBox1.DataSource = new BindingSource(userCache, null); comboBox1.DisplayMember = "Key"; comboBox1.ValueMember = "Value";
But why are you setting the ValueMember
to "Value", shouldn't it be bound to "Key" (and DisplayMember
to "Value" as well)?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With