DataGrid连接Access的快速分页法 5


DataGrid连接Access的快速分页法(5)——实现快速分页

我使用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" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGrid + DataReader 自定义分页</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="javascript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<form runat="server">
<ASP:datagrid id="DataGrid1" runat="server" BorderWidth="1px" BorderColor="Black" Font-Size="12pt"
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">
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
<ItemStyle Font-Size="Smaller" BorderWidth="22px"></ItemStyle>
<HeaderStyle BackColor="#AAAADD"></HeaderStyle>
<Columns>
<ASP:BoundColumn DataField="ID" SortExpression="ID" HeaderText="ID"></ASP:BoundColumn>
<ASP:BoundColumn DataField="订单ID" HeaderText="订单ID"></ASP:BoundColumn>
<ASP:BoundColumn DataField="产品ID" HeaderText="产品ID"></ASP:BoundColumn>
<ASP:BoundColumn DataField="单价" HeaderText="单价"></ASP:BoundColumn>
<ASP:BoundColumn DataField="数量" HeaderText="数量"></ASP:BoundColumn>
<ASP:BoundColumn DataField="折扣" HeaderText="折扣"></ASP:BoundColumn>
</Columns>
<PagerStyle Font-Names="VerDana" Font-Bold="True" HorizontalAlign="Right" ForeColor="Coral"
Mode="NumericPages"></PagerStyle>
</ASP:datagrid></form>
</body>
</HTML>


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
/// <summary>
/// 获取或设置当前页的索引。
/// </summary>
public int CurrentPageIndex
{
get { return (int) ViewState["CurrentPageIndex"]; }
set { ViewState["CurrentPageIndex"] = value; }
}
#endregion

#region OrderType
/// <summary>
/// 获取排序的方式:升序(ASC)或降序(DESC)。
/// </summary>
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);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

private void BindDataGrid()
{
#region 实现

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3