zgorawski
zgorawski

Reputation:

Sort dropdown list by text rather than value

I have dropdown list as follow:

<select id="DropdownID">
    <option value="1">Ccc</option>
    <option value="2">Aaa</option>
    <option value="3">Bbb</option>
</select>

but i want dropdown elements sorted by text not value, in alphabetical order:

<select id="DropdownID">
    <option value="2">Aaa</option>
    <option value="3">Bbb</option>
    <option value="1">Ccc</option>        
</select>

I fill it on this way:

Dictionary<int, string> myDataDictionary = new Dictionary<int, string>();

// (...)

DropdownID.DataSource = myDataDictionary;
DropdownID.DataTextField = "value";
DropdownID.DataValueField = "key";
DropdownID.DataBind();

Even if values in dictionary are sorted correctly, in dropdown they are always ordered by value.

How to sort items in dropdow by text rather than by value ?

Upvotes: 1

Views: 4908

Answers (4)

dmportella
dmportella

Reputation: 4724

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropdownID" runat="server">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Dictionary<int, string> myDataDictionary = new Dictionary<int, string>();

        myDataDictionary.Add(3, "A");
        myDataDictionary.Add(1, "B");
        myDataDictionary.Add(2, "C");

        DropdownID.DataSource = myDataDictionary;
        DropdownID.DataTextField = "value";
        DropdownID.DataValueField = "key";
        DropdownID.DataBind();

Hi there I have this working sample, what ever I do with the dictionary the drop down is ordered correctly.

could you post your working sample so we could reproduce your error plz.

Edit: 13:21 22/09/2009

What ever order you put your data in is the order that the dropdown list will use to display the collection. I think there must be something else that you are not aware off. The best way to find out if it is a problem with your code or something is truly amiss is to write a test app get something simple from scratch try to reproduce the behaviour of the original app.

Upvotes: 0

Waleed Eissa
Waleed Eissa

Reputation: 10513

You can't control how the items are ordered when using a dictionary, use a SortedDictionary instead.

Edit: I don't get what you mean by sorting the drop down by text, please give more details so that I can help you.

Edit2: Now that I see using a Dictionary is not important, I can say it all depends on how you retrieve the data from the data store (database, xml .. etc). How is your application structured? Is it a 3-tier application? If so, How do you retrieve the data from the DAL? If you're using Linq to SQL, it should be very easy (as indicated in another answer). Normally, you should retrieve the data already ordered from the database (assuming you retrieve the data from a database).

Upvotes: 0

Noam Gal
Noam Gal

Reputation: 3405

You can also use a SortedList<KeyValuePair<TKey, TValue>>, and use an IComparer<KeyValuePair<TKey, TValue> in the constructor (or Sort method) that compares using the value alone.
Not very nice, but it might work

Upvotes: 0

Paddy
Paddy

Reputation: 33857

Or if that's too much work, you can use LINQ and bind to an IOrderedEnumerable instead:

myDataDictionary.OrderBy(Function(item) item.Value)

(VB.net code - running a bit behind on writing this stuff in C#)

Upvotes: 3

Related Questions