排序 DropDownList?- C#, ASP.NET [英] Sorting a DropDownList? - C#, ASP.NET
问题描述
我很好奇在 C#/ASP.NET 中对 DropDownList 进行排序的最佳途径(更多的是寻求简单性,而不是速度或效率) - 我已经查看了一些建议,但它们并没有很好地点击我.
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.
伙计们,我无法控制数据如何进入 DropDownList - 我无法修改 SQL.
Folks, I do not have control over how the data comes into the DropDownList - I cannot modify the SQL.
推荐答案
如果你得到一个包含数据的 DataTable,你可以创建一个 DataView,然后将下拉列表绑定到那个.您的代码看起来像...
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...
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.
这篇关于排序 DropDownList?- C#, ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!