C# custom combobox sorting

后端 未结 3 2000
醉酒成梦
醉酒成梦 2021-01-13 16:55

Is it possible to have a custom sort/display order in a combox ? Say I want a special value \"MasterValue\" before all the others.

3条回答
  •  孤城傲影
    2021-01-13 17:31

    The following code will do the trick.

    1. Create a separate list of the items you want to sort and then use AddRange.

      comboBox1.Items.Add("Master");
      
      List listToSort = new List();
      
      listToSort.Add("6nd");
      listToSort.Add("3nd");
      listToSort.Add("5nd");
      listToSort.Add("4nd");
      listToSort.Add("2nd");
      
      listToSort.Sort();
      
      comboBox1.Items.AddRange(listToSort.ToArray());
      

提交回复
热议问题