首先,在.net的项目里新建一个DataGridPage.aspx文件,在aspx文件里拖入一个DataGrid控件。在控件上点击右键,选择“属性生成器”
选择分页项,勾上“允许分页”,点击确定。
进入cs页面,先加上“using System.Data.OleDb;”的引用。然后在“public class DataGridPage : System.Web.UI.Page”下加入“protected OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C://Program Files//Microsoft Office//OFFICE11//SAMPLES//Northwind.mdb");”
再在“private void Page_Load(object sender, System.EventArgs e)”里加入以下代码:
if (!this.IsPostBack)
{
DataGridBind();
}
然后建个DataGridBind方法如下:
private void DataGridBind()
{
string strSql = "select * from 产品";
DataSet ds = new DataSet();
conn.Open();
OleDbDataAdapter myAdapter = new OleDbDataAdapter(strSql,conn);
myAdapter.Fill(ds,"ds");
conn.Close();
DataView dv = ds.Tables[0].DefaultView;
this.DataGrid1.DataSource = dv;
this.DataGrid1.DataBind();
}
在DataGrid属性的PageIndexChang上双击,将会自动跳到cs文件上。
在cs文件里的DataGrid1_PageIndexChanged事件里输入以下代码:
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGridBind();
编译后预览一下,结果如下:
<%@ Page language="c#" Codebehind="DataGridPage.aspx.cs" AutoEventWireup="false" Inherits="test.DataGridPage" %>
<!
DOCTYPE
HTML
PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<HEAD>
<title>DataGridPage</title>
<metaname="GENERATOR"Content="Microsoft Visual Studio .NET 7.1">
<metaname="CODE_LANGUAGE"Content="C#">
<metaname="vs_defaultClientScript"content="JavaScript">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
<styletype="text/css"> BODY { FONT-SIZE: 9pt; BACKGROUND-COLOR: #ffffff } A:visited { COLOR: #000000; TEXT-DECORATION: none } A:active { COLOR: #000000; TEXT-DECORATION: none } A:hover { LEFT: 1px; COLOR: #000000; POSITION: relative; TOP: 1px; TEXT-DECORATION: underline } A:link { COLOR: #000000; TEXT-DECORATION: none } TD { FONT-SIZE: 9pt;"
宋体" } </style>
</HEAD>
<body>
<formid="Form1"method="post"runat="server">
<FONTface="
宋体">
<asp:DataGridid="DataGrid1"runat="server"AllowPaging="True"></asp:DataGrid></FONT>
</form>
</body>
</
HTML
>
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
using
System.Data.OleDb;
namespace
test
{
///<summary>
/// DataGridPage
的摘要说明。
///</summary>
public class DataGridPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C://Program Files//Microsoft Office//OFFICE11//SAMPLES//Northwind.mdb");
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
DataGridBind();
}
}
private void DataGridBind()
{
string strSql = "select * from
产品";
DataSet ds = new DataSet();
conn.Open();
OleDbDataAdapter myAdapter = new OleDbDataAdapter(strSql,conn);
myAdapter.Fill(ds,"ds");
conn.Close();
DataView dv = ds.Tables[0].DefaultView;
this.DataGrid1.DataSource = dv;
this.DataGrid1.DataBind();
}
#region
Web
窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:
该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///</summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGridBind();
}
}
}