我有这个Gridview,我有三列:ID
,Name
和LastName
我希望按名称Descending和LastName Ascending排序,但我不能正确地做到这一点。另外,忽略ListBox lstPeople,我只需要将它应用到Gridview grvPeople。谢谢!
aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Domain;
namespace WebInterface
{
public partial class myClass: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lstPeople.DataSource = controllerclass.Instance().getPeople();
lstPeople.DataTextField = "Data";
lstPeople.DataValueField = "ID";
lstPeople.DataBind();
grvPeople.DataSource =controllerclass.Instance().getPeople();
grvPeople.DataBind();
if (!IsPostBack)
{
List<People> myList = controllerclass.Instance().getPeople();
myList .Sort(new People.OrderbyName());
myList .Sort(new People.OrderbyLastName());
Session["sortedView"] = myList;
BindData(myList);
Session["filter"] = "Name";
Session["direction"] = SortDirection.Ascending;
}
}
protected void BindData(List<People> p)
{
lstPeople.DataSource = p;
lstPeople.DataBind();
}
protected void grvPeople_Sorting(object sender, GridViewSortEventArgs e)
{
List<People> myList = (List<People>)Session["sortedView"];
if (e.SortExpression == "ID") myList.Sort(new People.OrderbyID());
if (e.SortExpression == "Name") myList.Sort(new People.OrderbyName());
if (e.SortExpression == "LastName") myList.Sort(new People.OrderbyLastName());
if (e.SortExpression == Session["filter"].ToString().Trim())
{
SortDirection aux = (SortDirection)Session["direction"];
if (SortDirection.Ascending == aux)
{
Session["direction"] = SortDirection.Descending;
myList.Reverse();
}
else
{
Session["direction"] = SortDirection.Ascending;
}
}
Session["filter"] = e.SortExpression;
Session["sortedView"] = myList;
BindData(myList);
}
protected void grvPeople_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grvPeople.PageIndex = e.NewPageIndex;
BindData((List<People>)Session["sortedView"]);
}
}
}