Hendra Anggrian Hendra Anggrian - 1 month ago 10
C# Question

C# - is it possible to arrange ComboBox Items from a to z?

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?

Answer

There are two easy ways that I could think of:

  • If you're using Win Forms, just use ComboBox.Sorted = true;

  • If the data in your combo box comes from in a form of a list, just use OrderBy to the List of data you are going to put in the ComboBox before putting it.

example:

        List<string> a = new List<string>()
        {
            "q",
            "w",
            "e",
            "r",
            "t",
            "y",
            "u",
            "i",
            "o",
            "p",
            "a",
            "s",
            "d",
            "f",
            "g",
            "h",
            "j",
            "k",
            "l",
            "z",
            "x",
            "c",
            "v",
            "b",
            "n",
            "m",
        };

        comboBox1.Items.AddRange(a.OrderBy(c => c).ToArray());
Comments