如何对DropDownList进行排序

时间:2014-09-24 20:47:10

标签: c# asp.net sharepoint

我有以下代码填充DropDownList:

DataSet ds = new DataSet();
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
SPList list = myWeb.Lists["GuidelineTopics"];
DTable_List = list.Items.GetDataTable();
DTable_List.TableName = "Table1";
DTable_List.DefaultView.Sort = "Title ASC";
ds.Tables.Add(DTable_List);
Topic.DataSource = ds.Tables["Table1"];
Topic.DataSource = DTable_List;
Topic.DataTextField = "Title";
Topic.DataValueField = "Title";
Topic.DataBind();
Topic.Items.Insert(0, new ListItem("All Topics", "All Topics"));
Topic.SelectedIndex = 0;

如何将SORT应用于列表,使其按字母顺序排列为ASC或DESC?

1 个答案:

答案 0 :(得分:5)

尝试以下方法。你可以使用Linq OrderBy来获得你想要的东西。

如果要按列值的升序对数据源进行排序,请执行

Topic.DataSource = ds.Tables["Table1"].OrderBy(x => x.Title);

或者,如果您想按特定列名称的降序排序,请执行

Topic.DataSource = ds.Tables["Table1"].OrderByDescending(x => x.Title);