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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Sep 14, 2011, 1:23 AM
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
Prabhu RajaPosted Sep 14, 2011, 12:54 AM
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"