GridView中的双向排序
1. 设置GridView 的属性 AllowSorting="True" 否则没有可排序的下横线.
2. 邦定要序排的字段
2. 邦定要序排的字段
<asp:BoundField HeaderText="货号" DataField="ch_MaterialID" SortExpression="ch_MaterialID">
<ItemStyle Height="10px" Width="100px" />
</asp:BoundField>
3. 写排序的事件代码:<ItemStyle Height="10px" Width="100px" />
</asp:BoundField>
//在排序发生前激发
protected void GVStorageMaterial_Sorting(object sender, GridViewSortEventArgs e)
{
string strPage = e.SortExpression.ToString();
switch (strPage)
{
case "ch_ImageID":
ViewState["SortOrder"] = "ch_ImageID";
break;
case "ch_MaterialID":
ViewState["SortOrder"] = "ch_MaterialID";
break;
case "ch_MaterialName":
ViewState["SortOrder"] = "ch_MaterialName";
break;
}
//string sPage = e.SortExpression;
if (ViewState["OrderDire"] == null)
{
ViewState["OrderDire"] = "ASC";
}
if (ViewState["OrderDire"].ToString() == "ASC")
ViewState["OrderDire"] = "DESC";
else
ViewState["OrderDire"] = "ASC";
string strSQLSel = StrSqlSearch();
this.InitializeGridview(strSQLSel);
}
protected void GVStorageMaterial_Sorting(object sender, GridViewSortEventArgs e)
{
string strPage = e.SortExpression.ToString();
switch (strPage)
{
case "ch_ImageID":
ViewState["SortOrder"] = "ch_ImageID";
break;
case "ch_MaterialID":
ViewState["SortOrder"] = "ch_MaterialID";
break;
case "ch_MaterialName":
ViewState["SortOrder"] = "ch_MaterialName";
break;
}
//string sPage = e.SortExpression;
if (ViewState["OrderDire"] == null)
{
ViewState["OrderDire"] = "ASC";
}
if (ViewState["OrderDire"].ToString() == "ASC")
ViewState["OrderDire"] = "DESC";
else
ViewState["OrderDire"] = "ASC";
string strSQLSel = StrSqlSearch();
this.InitializeGridview(strSQLSel);
}
4. 在显示GridView的视图中设置排序的属性.
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
bool flagSel = objDBAccess.SearchSQL(strSQLSel, out objDS, out strMessage);
if (flagSel)
{
DataView dv=objDS.Tables[0].DefaultView;
dv.Sort = strSort;
GV.DataSource = dv;
GV.DataBind();
}
if (flagSel)
{
DataView dv=objDS.Tables[0].DefaultView;
dv.Sort = strSort;
GV.DataSource = dv;
GV.DataBind();
}