问题
I have a very messy long items full of strings in a combobox, and it would be lovely to just sort it from a to z to make it easier to track. Is it possible?
回答1:
There are two possible ways that I could think of:
A) Use the WinForms Combobox Sorted Property
If you're using WinForms, you can use ComboBox.Sorted = true;
B) Manually Sort your List with OrderBy
If the data in your combo box comes from in a form of a list, use OrderBy to the List of data you are going to put in the ComboBox before setting it.
Here's an example:
var myList = new List<string>() {"q","w","e","r","t","y"};
var sorted = a.OrderBy(c => c).ToArray()
comboBox1.Items.AddRange(sorted);
回答2:
turns out I can answer my own question, in Windows 8 C# app, ComboBox has Sort() properties that would simply arrange every items from a to z. Thanks.
回答3:
first I did not populate my dropdown combobox directly like this
AppCombBox1.Items.Add(comboboxvalue);
but instead i populated a list with same data (done in loop in code, which is searching through ini file for values):
List<string> TempList = new List<string>();
TempList.Add(comboboxvalue);
after list is populated, I sort it and after that I fill dropdown combobox with sorted values.
TempList.Sort();
foreach (string ListValue in TempList)
{
AppCombBox1.Items.Add(ListValue);
}
回答4:
Just set the Sorted Property to True. It will sort the elements. I am fetching values from database and it works fine.
来源:https://stackoverflow.com/questions/17080830/c-sharp-is-it-possible-to-arrange-combobox-items-from-a-to-z