GridView 分页 排序
HTML部分示例代码
—————-
<body>
<form id=”form1″ runat=”server”>
<asp:GridView ID=”GridView1″ runat=”server”
AllowPaging=”True” AllowSorting=”True” PageSize=”2″ AutoGenerateColumns=”False” OnPageIndexChanging=”GridView1_PageIndexChanging” OnSorting=”GridView1_Sorting”>
<Columns>
<asp:BoundField DataField=”TestID” HeaderText=”TestID” SortExpression=”TestID” />
<asp:BoundField DataField=”TestUser” HeaderText=”TestUser” SortExpression=”TestUser” />
<asp:BoundField DataField=”TestTime” HeaderText=”TestTime” SortExpression=”TestTime” />
</Columns>
</asp:GridView>
</form>
</body>
cs部分示例代码
————–
public partial class gv : System.Web.UI.Page
{
#region Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.Attributes.Add(“SortExpression”, “TestID”);
this.GridView1.Attributes.Add(“SortDirection”, “ASC”);
//绑定GridView
fn_DataBind();
}
}
#endregion
#region 得到绑定GridView的DataTable
private DataTable fn_getDataTable()
{
string strSql = ” SELECT TestID, TestUser, TestTime FROM Test “;
DBClass db = new DBClass();
DataSet ds = db.RunSelectGetDataSet(strSql);
return ds.Tables[0];
}
#endregion
#region 分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
fn_DataBind();
}
#endregion
#region 排序
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (e.SortExpression != “”)
{
if (GridView1.PageCount > 0)
{
//设定排序方向
string SortDirection = “ASC”;
SortDirection = (this.GridView1.Attributes[“SortDirection”].ToString() == SortDirection ? “DESC” : “ASC”);
this.GridView1.Attributes[“SortExpression”] = e.SortExpression;
this.GridView1.Attributes[“SortDirection”] = SortDirection;
//重新绑定数据
fn_DataBind();
}
}
}
#endregion
#region 绑定GridView
private void fn_DataBind()
{
DataTable dt = fn_getDataTable();
string SortDirection = this.GridView1.Attributes[“SortDirection”].ToString();
string SortExpression = this.GridView1.Attributes[“SortExpression”].ToString();
dt.DefaultView.Sort = string.Format(“{0} {1}”, SortExpression, SortDirection);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
#endregion
}
今天的文章GridView 分页 排序分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/27747.html