首页 > 数据库 > Access > 正文

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

2024-09-07 19:04:58
字体:
来源:转载
供稿:网友
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 实现
// 设置当前页的索引
datagrid1.currentpageindex = currentpageindex;
// 设置数据源
datagrid1.datasource = getdataview();
datagrid1.databind();
#endregion
}

/// <summary>
/// 取得数据库表中的记录总数。
/// </summary>
/// <param name="tablename">数据库中的表的名称。</param>
/// <returns>成功则为表中记录的总数;否则为 -1。</returns>
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
}
}
}

国内最大的酷站演示中心!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表