I am trying to figure out something. I have a method which adds some items into a ComboBox named “cbSize”. I realize that if I add two types of data into it, the code will crash. Is this because a ComboBox can only accommodate one type of data?
items.Add(1);
items.Add(10);
items.Add(100);
items.Add(2);
items.Add(20);
items.Add(3);
items.Add(30); //works fine if add numbers only
//items.Add("4"); //will crash if mix both numbers and text
//items.Add("2"); //works fine if add text only
//then sort them out
items.Sort();
//now clear original cbSize items
cbSize.Items.Clear();
//and add them back in sorted order
cbSize.Items.AddRange(items.ToArray());
//gotta clear ArrayList for the next time or else things will add up
items.Clear();
No, try below it will work
problem is with your items collection, it is type safe. you can’t add different types to it.
try with list of objects. it will work. reason is both int and string are objects
OR you can use ArrayList class (not type-safe because it can store any object)