Hi,
How to sort the item in a dropdownlist in asp.net ?
Hi,
How to sort the item in a dropdownlist in asp.net ?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Prabhu RajaRank #861 · 2.6K reputationPosted Sep 13, 2011, 8:54 PM EDT
Right Click DropdownList and Goto Properties -> You can find a Property names SORTED -> Make this property from false to true. it will automatically sort all the items
-------------------------------------------------------------------
If this post helps you, then mark as "Correct Answer"
Satyapriya NayakRank #47 · 31.5K reputationPosted Sep 13, 2011, 9:23 PM EDT
Here is your solution
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.Items.Add("raj");
DropDownList1.Items.Add("Berry");
DropDownList1.Items.Add("Arun");
DropDownList1.Items.Add("Crish");
SortDDL(ref this.DropDownList1);
}
private void SortDDL(ref DropDownList objDDL)
{
ArrayList textList = new ArrayList();
ArrayList valueList = new ArrayList();
foreach (ListItem li in objDDL.Items)
{
textList.Add(li.Text);
}
textList.Sort();
foreach (object item in textList)
{
string value = objDDL.Items.FindByText(item.ToString()).Value;
valueList.Add(value);
}
objDDL.Items.Clear();
for (int i = 0; i < textList.Count; i++)
{
ListItem objItem = new ListItem(textList[i].ToString(), valueList[i].ToString());
objDDL.Items.Add(objItem);
}
}
}
Thanks
If this post helps you mark it as answer