欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何在DataGrid控件中實(shí)現(xiàn)自定義分頁

 更新時(shí)間:2006年10月08日 00:00:00   作者:  
如何在DataGrid控件中實(shí)現(xiàn)自定義分頁 
    在一般情況下,DataGrid控件每次實(shí)現(xiàn)翻頁操作時(shí),都會(huì)將數(shù)據(jù)源中的數(shù)據(jù)重新調(diào)用一次,當(dāng)數(shù)據(jù)中
數(shù)據(jù)很多時(shí),這樣做就會(huì)很浪費(fèi)系統(tǒng)資源和降低程序的執(zhí)行效率.這時(shí)候我們一般通過自定義分頁來解
決這個(gè)問題.
    DataGrid控件的AllowCustomPaging屬性用來獲取或設(shè)置DataGrid控件是否允許自定義分
頁;VirtualItemCoun屬性用來獲取或設(shè)置在使用自定義分頁時(shí)DataGrid中實(shí)際的項(xiàng)數(shù).要實(shí)現(xiàn)自定義分
頁,必須將AllowPaging與AllowCustomPaging屬性都設(shè)置為"True".
    在DataGrid中要實(shí)現(xiàn)自定義分頁的關(guān)鍵是,使該控件僅僅調(diào)用當(dāng)前顯示所需要的數(shù)據(jù)源數(shù)據(jù),在下
面的例子中通過CurrentPageIndex和PageSize屬性的值,在數(shù)據(jù)綁定時(shí)只取當(dāng)前頁需要的數(shù)據(jù).
    (1)頁面代碼:
復(fù)制代碼 代碼如下:

<%@ 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)后臺(tái)代碼:
復(fù)制代碼 代碼如下:

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;//用來保存當(dāng)前頁數(shù)據(jù)項(xiàng)的起始索引
        private void Page_Load(object sender, System.EventArgs e)
        {
            if(!IsPostBack)
            {
                Bind();//初始化時(shí)進(jìn)行數(shù)據(jù)綁定
            }

        }
        void Bind()//綁定數(shù)據(jù)方法
        {  //定義數(shù)據(jù)庫連接對(duì)象
            SqlConnection cn=new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
           //創(chuàng)建數(shù)據(jù)適配對(duì)象
            SqlDataAdapter da=new SqlDataAdapter("select title_id ,title ,type, pub_id 
,price,pubdate from titles",cn);
            //創(chuàng)建DataSet對(duì)象
            DataSet ds=new DataSet();
            try
            {   //從指定的索引開始取PageSize條記錄.
                da.Fill(ds,startIndex,DataGrid1.PageSize,"CurDataTable");
                da.Fill(ds,"AllDataTable");//填充數(shù)據(jù)集合
                //設(shè)置DataGrid控件實(shí)際要顯示的項(xiàng)數(shù)
                DataGrid1.VirtualItemCount=ds.Tables["AllDataTable"].Rows.Count;
                //進(jìn)行數(shù)據(jù)綁定
                DataGrid1.DataSource=ds.Tables["CurDataTable"];
                DataGrid1.DataBind();

            }
            catch
            {
                Page.RegisterClientScriptBlock("","<script>alert('數(shù)據(jù)顯示錯(cuò)
誤');</script>");
            }

        }

        Web 窗體設(shè)計(jì)器生成的代碼#region Web 窗體設(shè)計(jì)器生成的代碼
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計(jì)器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /**//// <summary>
        /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
        /// 此方法的內(nèi)容。
        /// </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)
        {
                //設(shè)置DataGrid當(dāng)前頁的索引值為用戶選擇的頁的索引
                DataGrid1.CurrentPageIndex=e.NewPageIndex;
                //取得當(dāng)前頁為止總共有多少條記錄,以便在下一頁就從該記錄開始讀取
                startIndex=DataGrid1.PageSize*DataGrid1.CurrentPageIndex;
               //取得綁定數(shù)據(jù)
                Bind();
        }
    }
}

    在這段程序中,首先在數(shù)據(jù)綁定時(shí),設(shè)置DataGrid控件的VirtualItemCoun屬性值為查詢結(jié)果集中的
記錄總數(shù),然后取得當(dāng)前頁要顯示的數(shù)據(jù),初始化時(shí)當(dāng)前頁顯示的數(shù)據(jù)為從取到的數(shù)據(jù)的零位置開始,到
DataGrid控件的PageSize屬性的設(shè)定值為止的數(shù)據(jù)記錄數(shù).在分頁操作中重新定義了取得下一頁數(shù)據(jù)時(shí)
的數(shù)據(jù)項(xiàng)的開始索引值,然后調(diào)用數(shù)據(jù)綁定方法把取到的新數(shù)據(jù)和DataGrid控件重新綁定. 

相關(guān)文章

最新評(píng)論