首页  ·  知识 ·  云计算
DataGrid连接Access的快速分页法:实现快速分页
佚名  本站原创  综合  编辑:dezai  图片来源:网络
我使用Access自带的Northwind中文数据库的“订单明细”表作为例子,不过我在该表添加了一个名为“Id”的字段,数据类型为“自动编号”,并把该表命名为“订单明
我使用Access自带的Northwind中文数据库的“订单明细”表作为例子,不过我在该表添加了一个名为“Id”的字段,数据类型为“自动编号”,并把该表命名为“订单明细表”。
 
FastPaging_DataSet.aspx
--------------------------------------------------------------------------------------
<%@ Page language="c#" Codebehind="FastPaging_DataSet.aspx.cs" AutoEventWireup="false" Inherits="Paging.FastPaging_DataSet" EnableSessionState="False" enableViewState="True" enableViewStateMac="False" %>
      
              DataGrid + DataReader 自定义分页
             
             
             
             
      
      
             
                    
                            AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="#aaaadd" PagerStyle-HorizontalAlign="Right"
                            CellPadding="3" AllowPaging="True" AllowCustomPaging="True" AutoGenerateColumns="False" OnPageIndexChanged="MyDataGrid_Page"
                            PageSize="15" AllowSorting="True" OnSortCommand="DataGrid1_SortCommand">
                           
                           
                           
                           
                                  
                                  
                                  
                                  
                                  
                                  
                           
                           
                                   Mode="NumericPages">
                    
      
 
 
FastPaging_DataSet.aspx.cs
--------------------------------------------------------------------------------------
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;
using System.Text;
 
namespace Paging
{
    public class FastPaging_DataSet : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
 
        const String QUERY_FIELDS       = "*";          //要查询的字段
        const String TABLE_NAME     = "订单明细表"; //数据表名称
        const String PRIMARY_KEY        = "ID";         //主键字段
        const String DEF_ORDER_TYPE = "ASC";            //默认排序方式
        const String SEC_ORDER_TYPE = "DESC";       //可选排序方式
        const String CONDITION      = "产品ID='AV-CB-1'";
 
        OleDbConnection conn;
        OleDbCommand cmd;
        OleDbDataAdapter da;
 
        #region 属性
 
        #region CurrentPageIndex
        ///
        /// 获取或设置当前页的索引。
        ///
        public int CurrentPageIndex
        {
            get { return (int) ViewState["CurrentPageIndex"]; }
            set { ViewState["CurrentPageIndex"] = value; }
        }
        #endregion
 
        #region OrderType
        ///
        /// 获取排序的方式:升序(ASC)或降序(DESC)。
        ///
        public String OrderType
        {
            get {
                String orderType = DEF_ORDER_TYPE;
                if (ViewState["OrderType"] != null) {
                    orderType = (String)ViewState["OrderType"];
                    if (orderType != SEC_ORDER_TYPE)
                        orderType = DEF_ORDER_TYPE;
                }
                return orderType;
            }
            set { ViewState["OrderType"] = value.ToUpper(); }
        }
        #endregion
 
        #endregion
   
        private void Page_Load(object sender, System.EventArgs e)
        {
            #region 实现
            String strConn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source="
                + Server.MapPath("Northwind.mdb");
            conn = new OleDbConnection(strConn);
            cmd = new OleDbCommand("",conn);
            da = new OleDbDataAdapter(cmd);
 
            if (!IsPostBack) {
                // 设置用于自动计算页数的记录总数
                DataGrid1.VirtualItemCount = GetRecordCount(TABLE_NAME);
 
                CurrentPageIndex = 0;
                BindDataGrid();
            }
            #endregion
        }
 
        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
       
        ///
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        ///
        private void InitializeComponent()
        {   
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion
 
        private void BindDataGrid()
        {
            #region 实现
            // 设置当前页的索引
            DataGrid1.CurrentPageIndex = CurrentPageIndex;
            // 设置数据源
            DataGrid1.DataSource = GetDataView();
            DataGrid1.DataBind();
            #endregion
        }
 
        ///
        /// 取得数据库表中的记录总数。
        ///
        /// 数据库中的表的名称。
        /// 成功则为表中记录的总数;否则为 -1。
        private int GetRecordCount(string tableName)
        {
            #region 实现
            int count;
            cmd.CommandText = "SELECT COUNT(*) AS RecordCount FROM " + tableName;
            try {
                conn.Open();
                count = Convert.ToInt32(cmd.ExecuteScalar());
            } catch(Exception ex) {
                Response.Write (ex.Message.ToString());
                count = -1;
            } finally {
                conn.Close();
            }
            return count;
            #endregion
        }
 
        private DataView GetDataView()
        {
            #region 实现
            int pageSize = DataGrid1.PageSize;
            DataSet ds = new DataSet();
            DataView dv = null;
           
            cmd.CommandText = FastPaging.Paging(
                pageSize,
                CurrentPageIndex,
                DataGrid1.VirtualItemCount,
                TABLE_NAME,
                QUERY_FIELDS,
                PRIMARY_KEY,
                FastPaging.IsAscending(OrderType) );
 
            try {
                da.Fill(ds, TABLE_NAME);
                dv = ds.Tables[0].DefaultView;
            } catch(Exception ex) {
                Response.Write (ex.Message.ToString());
            }
            return dv;
            #endregion
        }
 
        protected void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e)
        {
            CurrentPageIndex = e.NewPageIndex;
            BindDataGrid();
        }
 
        protected void DataGrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            #region 实现
            DataGrid1.CurrentPageIndex = 0;
            this.CurrentPageIndex = 0;
 
            if (OrderType == DEF_ORDER_TYPE)
                OrderType = SEC_ORDER_TYPE;
            else
                OrderType = DEF_ORDER_TYPE;
 
            BindDataGrid();
            #endregion
        }
    }
}
本文作者:佚名 来源:本站原创
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读