2014-09-24 59 views
3

我有下面的代码填充一个DropDownList:如何排序一个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; 

我如何应用排序的列表,以便它要么ASC或DESC按字母顺序?

+0

你指的是哪个列表?看看Linq http://msdn.microsoft.com/en-us/library/vstudio/bb534966(v=vs.100).aspx – Vadim 2014-09-24 20:50:10

+0

'主题'是我的DropDownList – SearchForKnowledge 2014-09-24 20:50:35

回答

5

请尝试以下操作。你可以使用Linq OrderBy来得到你想要的。

如果要排序的列值的升序数据源,然后做

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

或者,如果你想在一个特定的列名降序排序,然后做

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

谢谢。 'x'和'x.ColumnName'是预定义的? – SearchForKnowledge 2014-09-24 20:54:38

+1

这是您的'fieldname'(标题)或您想要排序的数据源中的任何其他列。可能是我会用实际的字段名称编辑我的答案。 – 2014-09-24 20:57:31

+1

记得在你的代码中加入'using System.Linq;'来利用linq特性。 – 2014-09-24 21:04:04

相关问题