0

I'm new to C# and I'm having a little trouble sorting a dropdown list that's bound to a SharePoint list. 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-");

    }
Nick9one1
  • 1
  • 1

1 Answers1

0

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

Or if you can sort it with linq, not tested which should return an IEnumerable<YourServiceClass>

list.OrderBy(service => service.Title);
Community
  • 1
  • 1
auo
  • 572
  • 1
  • 7
  • 16