Sorting a DropDownList? - C#, ASP.NET

前端 未结 23 1055
慢半拍i
慢半拍i 2020-12-08 19:17

I\'m curious as to the best route (more looking towards simplicity, not speed or efficiency) to sort a DropDownList in C#/ASP.NET - I\'ve looked at a few recommendations b

23条回答
  •  -上瘾入骨i
    2020-12-08 20:10

    Another option is to put the ListItems into an array and sort.

            int i = 0;
            string[] array = new string[items.Count];
    
            foreach (ListItem li in dropdownlist.items)
            {
                array[i] = li.ToString();
                i++;
    
            }
    
            Array.Sort(array);
    
            dropdownlist.DataSource = array;
            dropdownlist.DataBind();
    

提交回复
热议问题