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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to alphabetically arrange dropdownlist

Status
Not open for further replies.

pinkpoppy

Programmer
Jul 6, 2011
75
US
Hello, I have a program that I have written and it works fine, but I am trying to get my dropdownlist to arrange the data alphabetically. When the user searches a number or name in a text box, a dropdownlist is populated with data from a web service. The data is all jumbled up. Any help is very much appreciated. Here's a look at a part of my codes:

protected void Page_Init(object sender, EventArgs e)
{

if (!IsPostBack)
{
string vals = Convert.ToString(Request.Form["txtValues"]);

Service newservice = new Service();

newservice.getStringList(vals);
ddlList.DataSource = PCHS.NameAppealNumber(vals);
ddlList.DataTextField = "Name";
ddlList.DataValueField = "ID";

ddlList.DataBind();


ListItem myitem = new ListItem();
myitem.Text = ".....";
myitem.Value = "";
ddlList.Items.Insert(0, myitem);

ddlList.SelectedIndex = 0;
}

}
 
Did you write the webservice? If you can change it, then have the webservice alphabitize it for you, if not, you will have to write your own sorting algorithim before you bind to the ddl.

And what does this do?[
Code:
 PCHS.NameAppealNumber(vals);
Perhaps you can do it in that method?
 
sort the data before returning it from the web service. if you're accessing a database it can be done at the database level. otherwise sort the results in memory before returning.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thank you so much for you replies.

Yes, jbenson001 and jmeckley .. I did write the web service. That is my question, how do I get the webservice to alphabitize it?

PCHS is the name of the web reference.
 
Here is a look at my web service code:

public class Service : System.Web.Services.WebService
{
public Service()
{

}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public List<Values> getStringList(string inp)
{
List<Values> vals = new List<Values>();
string[] strs = inp.Split(',');
foreach (string st in strs)
{
Values v = new Values();
v.name = st;
v.value = st;
vals.Add(v);

}
return vals;

}
 
what do you mean how? you sort it. using tsql, List.Sort(), Array.Sort(), linq's OrderBy() or DataView.Sort = "column ASC".

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
vals.Sort(v=>v.mame);
return vals;

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
public List<Values> getStringList(string inp)
{
List<Values> vals = new List<Values>();
string[] strs = inp.Split(',');
foreach (string st in strs)
{
Values v = new Values();
v.name = st;
v.value = st;
vals.Add(v);


}

vals.Sort(v=> v.name);
return vals;
}

(v=>v.name); is giving me an error: Delegate 'System.Comparison<application.Values>'does not take '1' arguments
 
then use the linq OrderBy() and ToList()

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
you can also simplify the code using linq
Code:
return inp
   .Split(',')
   .OrderBy(s => s)
   .Select(s => new Values {name = s, value = s})
   .ToList();

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks for your reply. The piece of code is not working. The dropdownlist is being populated by a web service which is an oracle database. I will try to sort it using sql. thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top