点击GridView的标题,实现相应字段的正向、逆向排序。
1. 首先设置GridView的属性,触发GridView的事件。
设置GridView的AllowSorting属性为True, 设置各列的SortExpression为相应的列标题。
Code
<Columns>
<asp:BoundField DataField="PersonID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="PersonName" HeaderText="姓名" SortExpression="姓名" />
<asp:BoundField DataField="GroupName" HeaderText="分组" SortExpression="分组" />
<asp:BoundField DataField="CellPhone" HeaderText="手机" SortExpression="手机" />
<asp:BoundField DataField="Remark" HeaderText="备注" SortExpression="备注" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<Columns>
<asp:BoundField DataField="PersonID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="PersonName" HeaderText="姓名" SortExpression="姓名" />
<asp:BoundField DataField="GroupName" HeaderText="分组" SortExpression="分组" />
<asp:BoundField DataField="CellPhone" HeaderText="手机" SortExpression="手机" />
<asp:BoundField DataField="Remark" HeaderText="备注" SortExpression="备注" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
触发GridView的Sorting事件。
2. 后台代码:
页面载入:
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//页面载入时,是按照PersonID的顺序排列
ViewState["PersonID"] = "PersonID";
ViewState["OrderDir"] = "asc";
//绑定数据
BindAll();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//页面载入时,是按照PersonID的顺序排列
ViewState["PersonID"] = "PersonID";
ViewState["OrderDir"] = "asc";
//绑定数据
BindAll();
}
}
绑定数据:
Code
private void BindAll()
{
//读取数据库中的数据列表
DataTable dt = dalPerson.SelectAll();
DataView view = dt.DefaultView;
string sort = (string)ViewState["PersonID"] + " " + (string)ViewState["OrderDir"];
view.Sort = sort ;
GrdPerson.DataSource = view;
//绑定数据
GrdPerson.DataBind();
}
private void BindAll()
{
//读取数据库中的数据列表
DataTable dt = dalPerson.SelectAll();
DataView view = dt.DefaultView;
string sort = (string)ViewState["PersonID"] + " " + (string)ViewState["OrderDir"];
view.Sort = sort ;
GrdPerson.DataSource = view;
//绑定数据
GrdPerson.DataBind();
}
Sorting事件:
Code
protected void GrdPerson_Sorting(object sender, GridViewSortEventArgs e)
{
string orderStr = "";
switch (e.SortExpression)
{
case "ID": orderStr = "PersonID"; break;
case "姓名": orderStr = "PersonName"; break;
case "分组": orderStr = "GroupName"; break;
case "手机": orderStr = "CellPhone"; break;
case "备注": orderStr = "Remark"; break;
}
ViewState["PersonID"] = orderStr;
if (ViewState["OrderDir"].ToString() == "desc")
{
ViewState["OrderDir"] ="ASC";
}
else
{
ViewState["OrderDir"] = "desc";//asc
}
BindAll();
}
protected void GrdPerson_Sorting(object sender, GridViewSortEventArgs e)
{
string orderStr = "";
switch (e.SortExpression)
{
case "ID": orderStr = "PersonID"; break;
case "姓名": orderStr = "PersonName"; break;
case "分组": orderStr = "GroupName"; break;
case "手机": orderStr = "CellPhone"; break;
case "备注": orderStr = "Remark"; break;
}
ViewState["PersonID"] = orderStr;
if (ViewState["OrderDir"].ToString() == "desc")
{
ViewState["OrderDir"] ="ASC";
}
else
{
ViewState["OrderDir"] = "desc";//asc
}
BindAll();
}