[英]Sorting a databound dropdownlist
I'm new to C# and I'm having a little trouble sorting a dropdown list that's bound to a SharePoint list. 我是C#的新手,在排序绑定到SharePoint列表的下拉列表时遇到了一些麻烦。 The dropdown seems to be ordered by the created date of each item in the sharepoint list, I need it sorted by title.
下拉列表似乎是按共享点列表中每个项目的创建日期排序的,我需要按标题对其进行排序。 Whats the easiest way to do this?
最简单的方法是什么?
Thanks! 谢谢!
protected void Page_Init(object sender, EventArgs e)
{
SPList list = SPContext.Current.Web.Lists["Services"];
//DataTable dt = list.Items.GetDataTable();
//dt.Rows.
DropDownList2.DataSource = list.Items.GetDataTable();
DropDownList2.DataTextField = "Title";
DropDownList2.DataBind();
Image1.Visible = false;
DropDownList2.AppendDataBoundItems = true;
this.DropDownList2.Items.Insert(0, "-Select-");
}
According to a previous answer at Sorting List Items (Latest one first) 根据先前对“排序列表项”的回答(最新到第一个)
SPList list = SPContext.Current.Web.Lists["Services"];
SPQuery q = new SPQuery();
q.Query = "<OrderBy><FieldRef Name='Title'/></OrderBy>";
SPListCollection listItemCollection = list.GetItems(q);
Also MSDN http://msdn.microsoft.com/en-us/library/office/ms457534(v=office.15).aspx for more information about SPQuery
也是MSDN http://msdn.microsoft.com/zh-cn/library/office/ms457534(v=office.15).aspx,以获得有关
SPQuery
更多信息
Or if you can sort it with linq, not tested which should return an IEnumerable<YourServiceClass>
或者,如果您可以使用linq 对其进行排序( 未经测试) ,则应返回
IEnumerable<YourServiceClass>
list.OrderBy(service => service.Title);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.