排序下拉列表?- C#、ASP.NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/222572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting a DropDownList? - C#, ASP.NET
提问by scrot
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.
我很好奇在 C#/ASP.NET 中对 DropDownList 进行排序的最佳途径(更多的是寻求简单性,而不是速度或效率) - 我已经查看了一些建议,但它们对我来说并不好。
Edit: Folks, I do not have control over how the data comes into the DropDownList - I cannot modify the SQL.
编辑:伙计们,我无法控制数据如何进入 DropDownList - 我无法修改 SQL。
采纳答案by Dillie-O
If you get a DataTable with the data, you can create a DataView off of this and then bind the drop down list to that. Your code would look something like...
如果你得到一个包含数据的 DataTable,你可以创建一个 DataView,然后将下拉列表绑定到那个。你的代码看起来像......
DataView dvOptions = new DataView(DataTableWithOptions);
dvOptions.Sort = "Description";
ddlOptions.DataSource = dvOptions;
ddlOptions.DataTextField = "Description";
ddlOptions.DataValueField = "Id";
ddlOptions.DataBind();
Your text field and value field options are mapped to the appropriate columnns in the data table you are receiving.
您的文本字段和值字段选项会映射到您正在接收的数据表中的相应列。
回答by MusiGenesis
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.
我通常使用数据库表中的值加载 DropDownList,因此最简单的方法是使用 SELECT 语句的 ORDER BY 子句根据需要对结果进行排序,然后遍历结果并将它们转储到 DropDownList 中。
回答by Jim
DropDownList takes any IEnumerable as a DataSource.
DropDownList 将任何 IEnumerable 作为数据源。
Just sort it using LINQ.
只需使用 LINQ 对其进行排序。
回答by bashmohandes
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 byclause, 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.
我同意人们在将数据填充到 DropDownList 之前对模型中的数据进行排序,因此如果您从数据库填充它,使用简单的order by子句将它们排序是一件好事,它会节省您在网络服务器中进行了一些循环,我相信数据库会做得更快。如果您从另一个数据源(例如 XML 文件)填充它,使用 LINQ 将是一个好主意,甚至 Array.Sort 的任何变体都将是好的。
回答by DOK
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.
如果您只想按字母顺序对显示的结果进行排序,我同意在填充数据库查询时使用 ORDER BY 进行排序。让数据库引擎做排序的工作。
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).
但是,有时您还需要除字母顺序之外的其他排序顺序。例如,您可能需要这样的逻辑顺序:新建、打开、进行中、已完成、已批准、已关闭。在这种情况下,您可以向数据库表中添加一列以显式设置排序顺序。将其命名为 SortOrder 或 DisplaySortOrder。然后,在您的 SQL 中,您将 ORDER BY 排序顺序字段(不检索该字段)。
回答by MusiGenesis
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.
如果您的数据是作为 System.Data.DataTable 提供给您的,请调用 DataTable 的 .Select() 方法,为 filterExpression 传入 "" 并为排序传入 "COLUMN1 ASC"(或您想要排序的任何列)。这将返回一个 DataRow 对象数组,按指定排序,然后您可以遍历这些对象并将其转储到 DropDownList 中。
回答by Samuel Kim
Take a look at the this article from CodeProject, 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.
看一下CodeProject中的这篇文章,它重新排列了下拉列表的内容。如果您是数据绑定,则需要在数据绑定到列表后运行排序器。
回答by sfuqua
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.
您使用哪种对象进行数据绑定?通常我使用 Collection<T>、List<T> 或 Queue<T>(视情况而定)。使用自定义委托对这些进行排序相对容易。请参阅关于比较(T) 委托的 MSDN 文档。
回答by SaaS Developer
Assuming you are running the latest version of the .Net Framework this will work:
假设您正在运行最新版本的 .Net Framework,这将起作用:
List<string> items = GetItemsFromSomewhere();
items.Sort((x, y) => string.Compare(x, y));
DropDownListId.DataSource = items;
DropDownListId.DataBind();
回答by Kon
You may not have access to the SQL, but if you have the DataSet or DataTable, you can certainly call the Sort()
method.
您可能无法访问 SQL,但如果您有 DataSet 或 DataTable,您当然可以调用该Sort()
方法。