In this blog we will know how to sort records in a
dropdownlist.
Default.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default" %>
<!DOCTYPE html
PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
</div>
<asp:DropDownList ID="DropDownList1"
runat="server">
</asp:DropDownList>
</form>
</body>
</html>
Default.aspx.cs code
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 Default
: 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");
dropSort(DropDownList1);
}
private void dropSort( DropDownList objdropSort)
{
ArrayList t1 = new ArrayList();
ArrayList t2 = new ArrayList();
foreach (ListItem l1 in
objdropSort.Items)
{
t1.Add(l1.Text);
}
t1.Sort();
{
string value =
objdropSort.Items.FindByText(ob1.ToString()).Value;
t2.Add(value);
}
objdropSort.Items.Clear();
for (int i = 0; i < t1.Count; i++)
{
ListItem t3 = new ListItem(t1[i].ToString(),
t2[i].ToString());
objdropSort.Items.Add(t3);
}
}
}
Thanks for reading