功能介绍:单击gridview的某一列列头,可以对该列进行排序。
实现方法:
1.设置AllowSorting="True"
2.在现实的字段中添加SortExpression属性,值为改字段的DataField
如:
<asp:BoundField DataField="BILLCODE" HeaderText="单据号" SortExpression="BILLCODE" />
<asp:BoundField DataField="CODE" HeaderText="货号" SortExpression="CODE" />
3.添加GridView的Sorting方法
现在Page_Load()方法中定义一个当前GV的排序顺序,并给出一个默认值:
ViewState["sortdirection"] = "ASC";
Sorting()方法中的代码如下:
protected void grvPro_Sorting(object sender, GridViewSortEventArgs e)
{
if (publicDS != null)
{
DataView dv = new DataView(publicDS);//publicDS为绑定GV的数据源
if (ViewState["sortdirection"].ToString() == "ASC")
{
dv.Sort = e.SortExpression + " DESC";
ViewState["sortdirection"] = "DESC";
}
else
{
dv.Sort = e.SortExpression + " ASC";
ViewState["sortdirection"] = "ASC";
}
grvPro.DataSource = dv;
grvPro.DataBind();
}
}