幽灵资源网 Design By www.bzswh.com
如何在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控件重新绑定.
幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。