Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 367 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting a DropDownList? - C#, ASP.NET

#1
I'm curious as to the best route (more looking towards simplicity, not speed or efficiency) to sort a DropDownList in C#/ASP.NET - I've looked at a few recommendations but they aren't clicking well with me.

Edit: Folks, I do not have control over how the data comes into the DropDownList - I cannot modify the SQL.
Reply

#2
I usually load a DropDownList with values from a database table, so the easiest way is to sort your results as desired with the ORDER BY clause of your SELECT statement, and then just iterate through the results and dump them into the DropDownList.
Reply

#3
DropDownList takes any IEnumerable as a DataSource.

Just sort it using LINQ.
Reply

#4
I agree with the folks in sorting your data in the model before populating them to the DropDownList, so if you are populating this from a DB, it is a good thing to get them sorted already there using a simple **order by** clause, it will save you some cycles in the web server, and I am sure the DB will do it so much faster.
If you are populating this from another data source for example, XML file, using LINQ will be a good idea, or even any variation of Array.Sort will be good.
Reply

#5
If your data is coming to you as a System.Data.DataTable, call the DataTable's .Select() method, passing in "" for the filterExpression and "COLUMN1 ASC" (or whatever column you want to sort by) for the sort. This will return an array of DataRow objects, sorted as specified, that you can then iterate through and dump into the DropDownList.
Reply

#6
Take a look at the [this article from CodeProject][1], which rearranges the content of a dropdownlist. If you are databinding, you will need to run the sorter after the data is bound to the list.


[1]:

[To see links please register here]

Reply

#7
I agree with sorting using ORDER BY when populating with a database query, if all you want is to sort the displayed results alphabetically. Let the database engine do the work of sorting.

However, sometimes you want **some other sort order besides alphabetical**. For example, you might want a logical sequence like: New, Open, In Progress, Completed, Approved, Closed. In that case, you could add a column to the database table to explicitly set the sort order. Name it something like SortOrder or DisplaySortOrder. Then, in your SQL, you'd ORDER BY the sort order field (without retrieving that field).

Reply

#8
What kind of object are you using for databinding? Typically I use Collection<T>, List<T>, or Queue<T> (depending on circumstances). These are relatively easy to sort using a custom delegate. See [MSDN documentation on the Comparison(T) delegate][1].


[1]:

[To see links please register here]

Reply

#9
Assuming you are running the latest version of the .Net Framework this will work:

List<string> items = GetItemsFromSomewhere();
items.Sort((x, y) => string.Compare(x, y));
DropDownListId.DataSource = items;
DropDownListId.DataBind();

Reply

#10
You may not have access to the SQL, but if you have the DataSet or DataTable, you can certainly call the `Sort()` method.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through