如何在DataGrid控件中实现自定义分页
更新时间:2006年10月08日 00:00:00 作者:
如何在DataGrid控件中实现自定义分页
在一般情况下,DataGrid控件每次实现翻页操作时,都会将数据源中的数据重新调用一次,当数据中
数据很多时,这样做就会很浪费系统资源和降低程序的执行效率.这时候我们一般通过自定义分页来解
决这个问题.
DataGrid控件的AllowCustomPaging属性用来获取或设置DataGrid控件是否允许自定义分
页;VirtualItemCoun属性用来获取或设置在使用自定义分页时DataGrid中实际的项数.要实现自定义分
页,必须将AllowPaging与AllowCustomPaging属性都设置为"True".
在DataGrid中要实现自定义分页的关键是,使该控件仅仅调用当前显示所需要的数据源数据,在下
面的例子中通过CurrentPageIndex和PageSize属性的值,在数据绑定时只取当前页需要的数据.
(1)页面代码:
<%@ Page language="c#" Codebehind="Main.aspx.cs" AutoEventWireup="false"
Inherits="SissonDemo.Main" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Main</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION:
absolute; TOP: 24px" runat="server"
Width="792px" Height="96px" AllowCustomPaging="True"
AllowPaging="True" PageSize="5">
<PagerStyle Mode="NumericPages"></PagerStyle>
</asp:DataGrid></FONT>
</form>
</body>
</HTML>
(2)后台代码:
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.SqlClient;
namespace SissonDemo
{
/**//// <summary>
/// Main 的摘要说明。
/// </summary>
public class Main : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
int startIndex = 0;//用来保存当前页数据项的起始索引
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bind();//初始化时进行数据绑定
}
}
void Bind()//绑定数据方法
{ //定义数据库连接对象
SqlConnection cn=new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
//创建数据适配对象
SqlDataAdapter da=new SqlDataAdapter("select title_id ,title ,type, pub_id
,price,pubdate from titles",cn);
//创建DataSet对象
DataSet ds=new DataSet();
try
{ //从指定的索引开始取PageSize条记录.
da.Fill(ds,startIndex,DataGrid1.PageSize,"CurDataTable");
da.Fill(ds,"AllDataTable");//填充数据集合
//设置DataGrid控件实际要显示的项数
DataGrid1.VirtualItemCount=ds.Tables["AllDataTable"].Rows.Count;
//进行数据绑定
DataGrid1.DataSource=ds.Tables["CurDataTable"];
DataGrid1.DataBind();
}
catch
{
Page.RegisterClientScriptBlock("","<script>alert('数据显示错
误');</script>");
}
}
Web 窗体设计器生成的代码#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)
{
//设置DataGrid当前页的索引值为用户选择的页的索引
DataGrid1.CurrentPageIndex=e.NewPageIndex;
//取得当前页为止总共有多少条记录,以便在下一页就从该记录开始读取
startIndex=DataGrid1.PageSize*DataGrid1.CurrentPageIndex;
//取得绑定数据
Bind();
}
}
}
在这段程序中,首先在数据绑定时,设置DataGrid控件的VirtualItemCoun属性值为查询结果集中的
记录总数,然后取得当前页要显示的数据,初始化时当前页显示的数据为从取到的数据的零位置开始,到
DataGrid控件的PageSize属性的设定值为止的数据记录数.在分页操作中重新定义了取得下一页数据时
的数据项的开始索引值,然后调用数据绑定方法把取到的新数据和DataGrid控件重新绑定.
在一般情况下,DataGrid控件每次实现翻页操作时,都会将数据源中的数据重新调用一次,当数据中
数据很多时,这样做就会很浪费系统资源和降低程序的执行效率.这时候我们一般通过自定义分页来解
决这个问题.
DataGrid控件的AllowCustomPaging属性用来获取或设置DataGrid控件是否允许自定义分
页;VirtualItemCoun属性用来获取或设置在使用自定义分页时DataGrid中实际的项数.要实现自定义分
页,必须将AllowPaging与AllowCustomPaging属性都设置为"True".
在DataGrid中要实现自定义分页的关键是,使该控件仅仅调用当前显示所需要的数据源数据,在下
面的例子中通过CurrentPageIndex和PageSize属性的值,在数据绑定时只取当前页需要的数据.
(1)页面代码:
复制代码 代码如下:
<%@ Page language="c#" Codebehind="Main.aspx.cs" AutoEventWireup="false"
Inherits="SissonDemo.Main" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Main</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION:
absolute; TOP: 24px" runat="server"
Width="792px" Height="96px" AllowCustomPaging="True"
AllowPaging="True" PageSize="5">
<PagerStyle Mode="NumericPages"></PagerStyle>
</asp:DataGrid></FONT>
</form>
</body>
</HTML>
(2)后台代码:
复制代码 代码如下:
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.SqlClient;
namespace SissonDemo
{
/**//// <summary>
/// Main 的摘要说明。
/// </summary>
public class Main : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
int startIndex = 0;//用来保存当前页数据项的起始索引
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bind();//初始化时进行数据绑定
}
}
void Bind()//绑定数据方法
{ //定义数据库连接对象
SqlConnection cn=new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
//创建数据适配对象
SqlDataAdapter da=new SqlDataAdapter("select title_id ,title ,type, pub_id
,price,pubdate from titles",cn);
//创建DataSet对象
DataSet ds=new DataSet();
try
{ //从指定的索引开始取PageSize条记录.
da.Fill(ds,startIndex,DataGrid1.PageSize,"CurDataTable");
da.Fill(ds,"AllDataTable");//填充数据集合
//设置DataGrid控件实际要显示的项数
DataGrid1.VirtualItemCount=ds.Tables["AllDataTable"].Rows.Count;
//进行数据绑定
DataGrid1.DataSource=ds.Tables["CurDataTable"];
DataGrid1.DataBind();
}
catch
{
Page.RegisterClientScriptBlock("","<script>alert('数据显示错
误');</script>");
}
}
Web 窗体设计器生成的代码#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)
{
//设置DataGrid当前页的索引值为用户选择的页的索引
DataGrid1.CurrentPageIndex=e.NewPageIndex;
//取得当前页为止总共有多少条记录,以便在下一页就从该记录开始读取
startIndex=DataGrid1.PageSize*DataGrid1.CurrentPageIndex;
//取得绑定数据
Bind();
}
}
}
在这段程序中,首先在数据绑定时,设置DataGrid控件的VirtualItemCoun属性值为查询结果集中的
记录总数,然后取得当前页要显示的数据,初始化时当前页显示的数据为从取到的数据的零位置开始,到
DataGrid控件的PageSize属性的设定值为止的数据记录数.在分页操作中重新定义了取得下一页数据时
的数据项的开始索引值,然后调用数据绑定方法把取到的新数据和DataGrid控件重新绑定.
您可能感兴趣的文章:
相关文章
在ASP.NET 2.0中操作数据之六十八:为DataTable添加额外的列
本文介绍并使用TableAdapter向DataTable添加新的一列的方法和步骤,任何时候只要重新运行TableAdapter设置向导,用户所做的所有定制都要被覆盖,为避免出现这种情况,我们建议直接修改存储过程。2016-05-05
在ASP.NET 2.0中操作数据之二十三:基于用户对修改数据进行限制
本文主要介绍ASP.NET 2.0如何限制用户修改数据,说白了,就是如何控制权限,让有权限的人才能修改,没有权限的人只能查看数据。2016-05-05
在ASP.NET 2.0中操作数据之三:创建母版页和站点导航
本文主要介绍在ASP.NET如何创建母版页以及站点地图等操作,母版页的实现的功能有点类似自定义用户控件,可以实现网站页面统一的设计和布局。2016-04-04
在ASP.NET 2.0中操作数据之十:使用 GridView和DetailView实现的主/从报表
本文我们主要研究了如何使用可选择行的GridView显示主记录,以及在DetailsView中显示选中记录的详细信息。2016-05-05
在ASP.NET 2.0中操作数据之六十三:GridView实现批量删除数据
本文主要介绍在GridView控件中包含一个checkbox列来实现复选多条数据,在批量删除按钮的事件中通过for循环来一一删除。2016-05-05
解读ASP.NET 5 & MVC6系列教程(8):Session与Caching
这篇文章主要介绍了ASP.NET 5 中的Session与Caching配置和使用,需要的朋友可以参考下2016-06-06
NopCommerce架构分析(一)Autofac依赖注入类生成容器
本文介绍了NopCommerce中IOC框架Autofac的使用,Autofac是一款IOC框架,比较于其他的IOC框架,如Spring.NET,Unity,Castle等等所包含的,它很轻量级性能上非常高。2016-04-04
在ASP.NET 2.0中操作数据之十一:基于数据的自定义格式化
GridView, DetailsView, FormView的格式自定义可以有多种方法, 在本文中我们将用DataBound 和 RowDataBound两种事件来完成,下面主要演示了货币、加粗、斜体、高亮的数据格式化。2016-05-05


最新评论