Linq学习——gridview实现双向排序
前端代码:添加一个gridview,将AllowSorting属性设置为true,在gridview每一列的SortExpression属性中添加相应的值,生成的代码如下:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" onsorting="GridView1_Sorting">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID"
SortExpression="EmployeeID" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="TitleOfCourtesy" HeaderText="TitleOfCourtesy"
SortExpression="TitleOfCourtesy" />
</Columns>
</asp:GridView>
后台调用代码:
private void BindEmp()
{
List<Employees> localDataTable;
localDataTable = objCategories.FindAllList(ViewState["SortOrder"].ToString(), ViewState["OrderDire"].ToString());
GridView1.DataSource = localDataTable;
GridView1.DataBind();
}
gridview 的Sorting 事件代码:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string spage = e.SortExpression;
if (ViewState["SortOrder"].ToString() == spage)
{
if (ViewState["OrderDire"].ToString() == "DESC")
{
ViewState["OrderDire"] = "ASC";
}
else
{
ViewState["OrderDire"] = "DESC";
}
}
else
{
ViewState["SortOrder"] = e.SortExpression;
}
BindEmp();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["SortOrder"] = "EmployeeID";
ViewState["OrderDire"] = "ASC";
BindEmp();
}
}
Bll层代码:
public List<Employees> FindAllList(string sordCol, string sortType)
{
return objDataAccess.FindAllList(sordCol, sortType);
}
DLL层代码:
public List<Employees> FindAllList(string sordCol, string sortType)
{
List<Employees> objList=new List<Employees>();
IEnumerable<Employees> custQuery = null;
switch (sordCol)
{
case "EmployeeID":
{
if (sortType == "ASC")
{
custQuery =
from em in nw.Employees
orderby em.EmployeeID
select em;
}
else if (sortType == "DESC")
{
custQuery =
from em in nw.Employees
orderby em.EmployeeID descending
select em;
}
}
break;
case "LastName":
{
&
补充:综合编程 , 其他综合 ,
上一个:基数排序之LSD篇 (知识点小结)
下一个:鸡尾酒排序算法