Click here to Skip to main content
15,888,282 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
I am using this code to sort out data from my database(ascending only), it does work when I am just only using it for my dropdown list with item inside it but doesn't work when using on my dropdown list which needs to get its content from the database.

here's the code:

//FROM THE PAGE_LOAD OF MY ASPX.CS

C#
SortedList slst = new SortedList();
foreach (ListItem li in drpSchoolOff.Items)
{
    slst.Add(li.Text, li.Value);
}
drpSchoolOff.DataSource = slst;
drpSchoolOff.DataValueField = "Value";
drpSchoolOff.DataTextField = "Key";
drpSchoolOff.DataBind();


//THE DROPDOWN LIST I'M USING FROM MY .ASPX

ASP.NET
<asp:DropDownList ID="drpSchoolOff" runat="server" Width="200px" Enabled="True">



What happens is it basically clear the dropdown I have tried removing drpSchoolOff.DataBind(); but the sorting still doesn't work, Let me just remind again that the dropdown is supposed to be using data from the database. Thanks in advanced.
Posted
Updated 14-Aug-13 14:57pm
v4

Hi,

You can checkout below code insted of database connection i created my own collection datasource.


// School entity which will define structure of entity
        public class Schools
        {
            public int OfficeID { get; set; }
            public String OfficeName { get; set; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Creating collection which will work as datasorce for dropdown
            //Initially it's in unsorted order.
            List<Schools> offices = new List<Schools>{ 
                new Schools{ OfficeID=2, OfficeName="USA"},
                new Schools{ OfficeID=4, OfficeName="IND"},
                new Schools{ OfficeID=1, OfficeName="SGP"},
                new Schools{ OfficeID=3, OfficeName="UK"},
            };

            //Using Lymbda expression we are creating our own asc or dec logic
            // if you want sort by name then use offices.OrderBy(x => x.OfficeName); 

            drpSchoolOff.DataSource = offices.OrderBy(x => x.OfficeID); 
            drpSchoolOff.DataValueField = "OfficeID";
            drpSchoolOff.DataTextField = "OfficeName";
            drpSchoolOff.DataBind();

        }
 
Share this answer
 
the loop doesn't do any actual sorting. a simple way to go by sorting lists of objects would be:

C#
...

...

{sortedlist} = {unsortedlist}.OrderBy(dc => dc.{columnOrattributevalueyouwanttosortby});

//or you could use the same unsortedlist instead of creating a new one.

{unsortedlist} = {unsortedlist}.OrderBy(dc => dc.{columnOrattributevalueyouwanttosortby});

...

//for the binding section, you may try to just set the itemsource of the dropdown list

drpSchoolOff.DataSource = {sortedlist} ;
drpSchoolOff.DataBind();

//or {unsorted} depending on current datasource.

...


Then bind the actual attributes of column values in .aspx

ASP.NET
...

<asp:DropDownList ID="drpSchoolOff" datatextfield="{attribute1}" DataValueField="{attribute2}" runat="server" Width="200px" Enabled="True">

...

</asp:dropdownlist>
 
Share this answer
 
v2
Declared outside:
<prev>
static List<schools> offices = new List<schools> { };

Then added this:
public class Schools
{
public int OfficeID { get; set; }
public String Description { get; set; }
}
And finally:
dtOffice = blSchoolT.loadSchoolAll();

foreach (DataRow r in dtOffice.Rows)
{

offices.Add(new Schools { OfficeID = Convert.ToInt32(r[0]), Description = r[2].ToString() });
}

drpSchoolOff.DataSource = offices.OrderBy(x => x.Description);
drpSchoolOff.DataValueField = "OfficeID";
drpSchoolOff.DataTextField = "Description";
drpSchoolOff.DataBind();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900