Sorting a DropDownList? - C#, ASP.NET

前端 未结 23 1146
慢半拍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条回答
  •  余生分开走
    2020-12-08 19:50

            List li = new List();
            foreach (ListItem list in DropDownList1.Items)
            {
                li.Add(list);
            }
            li.Sort((x, y) => string.Compare(x.Text, y.Text));
            DropDownList1.Items.Clear();
            DropDownList1.DataSource = li;
            DropDownList1.DataTextField = "Text";
            DropDownList1.DataValueField = "Value";
            DropDownList1.DataBind();
    

提交回复
热议问题