- 爱易网页
-
ASP.NET教程
- 散分:DataTable绑定到GridView怎么分页
日期:2014-05-18 浏览次数:20433 次
散分:DataTable绑定到GridView如何分页?
简单用例如下:
<%@ Page Language= "C# " %>
<%@ Import Namespace= "System.Data " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "server ">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.DataBind();
}
}
private void DataBind()
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn( "x ");
dt.Columns.Add(dc);
for (int i = 1; i < 25; i++)
{
DataRow dr = dt.NewRow();
dr[ "x "] = i;
dt.Rows.Add(dr);
}
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//???
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
</script>
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head runat= "server ">
<title> Untitled Page </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<asp:GridView ID= "GridView1 " runat= "server " AllowPaging= "True " OnPageIndexChanging= "GridView1_PageIndexChanging ">
</asp:GridView>
</form>
</body>
</html>