1.aspx前台代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="true" OnSorting="GridView1_Sorting" AllowPaging="true" PageSize="10" OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:BoundField DataField="deptid" HeaderText="部门代码" SortExpression="deptid"/>
<asp:BoundField DataField="deptename" HeaderText="部门名称" SortExpression="deptename"/>
<asp:TemplateField SortExpression="BUID" HeaderText="BUID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("BUID") %>' ToolTip='<%# DataBinder.Eval(Container.DataItem, "BUID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
2.aspx.cs后台代码
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ViewState["SortOrder"] = "deptid";
ViewState["OrderDire"] = "asc";
BindGridView();
}
}
protected void BindGridView()
{
DataSet ds = new DataSet();
ds = MIS.Users.GetDept();
DataView dv = ds.Tables[0].DefaultView;
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
dv.Sort = sort;
this.GridView1.DataSource = dv;
this.GridView1.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string vortExp = e.SortExpression;
if (ViewState["SortOrder"].ToString() == vortExp)
{
if ((string)ViewState["OrderDire"] == "desc")
{
ViewState["OrderDire"] = "asc";
}
else if ((string)ViewState["OrderDire"] == "asc")
{
ViewState["OrderDire"] = "desc";
}
}
else
{
ViewState["SortOrder"] = e.SortExpression;
}
BindGridView();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
BindGridView();
}