Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort items in a drop down list 1

Status
Not open for further replies.

AT76

Technical User
Apr 14, 2005
460
US
Hi all,

I have created a drop down list that display items from from an XML file. I would like to sort this items. I'm trying to figure out how to go about doing this.

First thing that jumps to mind is putting the items in an array then sorting the array. After that I would pass this array back to the list. Can anyone offer any ideas or example?

Thank you!

 
Here's the code I have that fills my dropdown:

Code:
    private void FillDropDown(string element) 
        {
            string name = "";
            this.selection.Items.Clear();
            System.Xml.XmlTextReader reader = new XmlTextReader(xmlPath);
            object firstNameObj = reader.NameTable.Add("Title");
           
            while (reader.Read())
            {
                if (reader.Name.Equals(firstNameObj))
                {
                    name = reader.ReadString();
                    ListItem item = new ListItem(name, name);
                    this.selection.Items.Add(item);
                }
            }
            reader.Close();
        }


Thanks! [smile]
 
i would create a seperate class/function which gets the data from the xml file and returns a sorted IEnumerable.

in your GUI bind the drop down list to the results of this function.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Alternatively you can just set the Sorted property of the list or combo box to true.

I believe that defaults to an ascending sort.
 
Thank you both...

jmeckley, I'm trying to follow your advice. I created a added a new class to my project called SortArray.cs

Unfortunately when trying to connect to the xml file:

Code:
xmlPath = Server.MapPath("File1.xml");

I get the following error:

Code:
The name 'Server' does not exist in the current context

Is there a way to overcome this? Also, I'm still not sure how to return the sorted IEnumerable to the default.aspx.

Thanks for your help.




 
Is this a web app? aspx pages?

Otherwise just specify the location to your file

Application.StartupPath + "File1.xml";
 
your function would look like this
Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.XML;

public static class MyClass
{
   public static IEnumberable GetNodeValues(string fileName, string xPathQuery)
   {
       XmlDocument doc = new XMLDocument();
       doc.Load(fileName);

       XMLNodeList nodes = doc.SelectNodes(xPathQuery);
       List<string> toReturn = new List<string>(nodes.Count);

       foreach(XMLNode node in nodes)
       {
          toReturn.Add(Node.Value);
       }
       return toReturn.ToArray();
   }
}
how to use
Code:
MyDropDownList.DataSource = MyClass.GetNodeValues(Server.MapPath("File1.xml"), "//Title");
for future reference the Server object is located here:
System.Web.HttpContext.Current.Server

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you Jason. I will give a try to your example! I appreciate your guidance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top