如何在DataGrid控件中实现自定义分页

本文介绍如何在DataGrid控件中实现自定义分页功能,以提高数据加载效率并节省系统资源。通过设置AllowCustomPaging及AllowPaging属性,并结合CurrentPageIndex和PageSize属性,仅加载当前页所需数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    在一般情况下,DataGrid控件每次实现翻页操作时,都会将数据源中的数据重新调用一次,当数据中数据很多时,这样做就会很浪费系统资源和降低程序的执行效率.这时候我们一般通过自定义分页来解决这个问题.
    DataGrid控件的AllowCustomPaging属性用来获取或设置DataGrid控件是否允许自定义分页;VirtualItemCoun属性用来获取或设置在使用自定义分页时DataGrid中实际的项数.要实现自定义分页,必须将AllowPaging与AllowCustomPaging属性都设置为"True".
    在DataGrid中要实现自定义分页的关键是,使该控件仅仅调用当前显示所需要的数据源数据,在下面的例子中通过CurrentPageIndex和PageSize属性的值,在数据绑定时只取当前页需要的数据.
    (1)页面代码:
None.gif<%@ Page language="c#" Codebehind="Main.aspx.cs" AutoEventWireup="false" Inherits="SissonDemo.Main" %>
None.gif
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
None.gif
<HTML>
None.gif    
<HEAD>
None.gif        
<title>Main</title>
None.gif        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
None.gif        
<meta name="CODE_LANGUAGE" Content="C#">
None.gif        
<meta name="vs_defaultClientScript" content="JavaScript">
None.gif        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
None.gif    
</HEAD>
None.gif    
<body MS_POSITIONING="GridLayout">
None.gif        
<form id="Form1" method="post" runat="server">
None.gif            
<FONT face="宋体">
None.gif                
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 24px" runat="server"
None.gif                    Width
="792px" Height="96px" AllowCustomPaging="True" AllowPaging="True" PageSize="5">
None.gif                    
<PagerStyle Mode="NumericPages"></PagerStyle>
None.gif                
</asp:DataGrid></FONT>
None.gif        
</form>
None.gif    
</body>
None.gif
</HTML>
None.gif
    (2)后台代码:
None.gifusing System;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Data;
None.gif
using System.Drawing;
None.gif
using System.Web;
None.gif
using System.Web.SessionState;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Data.SqlClient;
None.gif
None.gif
namespace SissonDemo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Main 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Main : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
InBlock.gif         
int startIndex = 0;//用来保存当前页数据项的起始索引
InBlock.gif
        private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Bind();
//初始化时进行数据绑定
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
void Bind()//绑定数据方法
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{  //定义数据库连接对象
InBlock.gif
            SqlConnection cn=new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
InBlock.gif           
//创建数据适配对象
InBlock.gif
            SqlDataAdapter da=new SqlDataAdapter("select title_id ,title ,type, pub_id ,price,pubdate from titles",cn);
InBlock.gif            
//创建DataSet对象
InBlock.gif
            DataSet ds=new DataSet();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{   //从指定的索引开始取PageSize条记录.
InBlock.gif
                da.Fill(ds,startIndex,DataGrid1.PageSize,"CurDataTable");
InBlock.gif                da.Fill(ds,
"AllDataTable");//填充数据集合
InBlock.gif                
//设置DataGrid控件实际要显示的项数
InBlock.gif
                DataGrid1.VirtualItemCount=ds.Tables["AllDataTable"].Rows.Count;
InBlock.gif                
//进行数据绑定
InBlock.gif
                DataGrid1.DataSource=ds.Tables["CurDataTable"];
InBlock.gif                DataGrid1.DataBind();
InBlock.gif            
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Page.RegisterClientScriptBlock(
"","<script>alert('数据显示错误');</script>");
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif                
//设置DataGrid当前页的索引值为用户选择的页的索引
InBlock.gif
                DataGrid1.CurrentPageIndex=e.NewPageIndex;
InBlock.gif                
//取得当前页为止总共有多少条记录,以便在下一页就从该记录开始读取
InBlock.gif
                startIndex=DataGrid1.PageSize*DataGrid1.CurrentPageIndex;
InBlock.gif               
//取得绑定数据
InBlock.gif
                Bind();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
    在这段程序中,首先在数据绑定时,设置DataGrid控件的VirtualItemCoun属性值为查询结果集中的记录总数,然后取得当前页要显示的数据,初始化时当前页显示的数据为从取到的数据的零位置开始,到DataGrid控件的PageSize属性的设定值为止的数据记录数.在分页操作中重新定义了取得下一页数据时的数据项的开始索引值,然后调用数据绑定方法把取到的新数据和DataGrid控件重新绑定.

转载于:https://www.cnblogs.com/zhouxiaxue/archive/2006/06/16/427809.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值