Welcome to Dream.In.Code
Become an Expert!

Join 150,396 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,038 people online right now. Registration is fast and FREE... Join Now!




Sorting a DropDownList

 
Reply to this topicStart new topic

Sorting a DropDownList

PsychoCoder
28 Feb, 2008 - 11:12 PM
Post #1

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
In my web application I have a DropDownList that is populated from a DataSet. Once the DropDownList is bound Im attempting to sort it using the following code, but it isnt working, and I cant figure out why


csharp

//first we bind the DropDwnList
DataSet domains = util.BuildDataSet(Server.MapPath("supported_domains.txt"), "Domains", "\t");
supported_domain.DataSource = domains;
supported_domain.DataTextField = domains.Tables[0].Columns[0].ToString();
supported_domain.DataValueField = domains.Tables[0].Columns[0].ToString();
supported_domain.DataBind();

//now we will sort the DropDownList
ListItem[] sorted = new ListItem[supported_domain.Items.Count];
//Response.Write(sorted.Length);
for (int i = 0; i < sorted.Length; i++)
{
sorted[i] = supported_domain.Items[i];
}
Array.Sort(sorted);
supported_domain.Items.Clear();
supported_domain.Items.AddRange(sorted);

User is online!Profile CardPM
+Quote Post

Nayana
RE: Sorting A DropDownList
28 Feb, 2008 - 11:27 PM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
It's probably something to do with the data binding, so you can't your own entries to it.

Have you tried setting the Sorted property to true? (assuming the DropDownList supports it).

This post has been edited by Nayana: 28 Feb, 2008 - 11:28 PM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Sorting A DropDownList
28 Feb, 2008 - 11:37 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
A DropDownList doesn't support sorting. Guess I can try to sort the items before adding them to the DataSet, but that just seems like a ton of work.

Well the code works on a non data bound DropDownList, so back to the drawing board lol
User is online!Profile CardPM
+Quote Post

Nayana
RE: Sorting A DropDownList
29 Feb, 2008 - 01:29 AM
Post #4

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
I don't actually have ASPX to play around with, so bear with me...

Would it be possible to unbind the DropDownList, then copy the array items back into it?

Does the array even get the items in the first place?
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Sorting A DropDownList
29 Feb, 2008 - 09:01 AM
Post #5

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Yeah the Array gets the items, I had it print out it's length and it was the correct length
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 06:20PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month