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
}
}
}
國內最大的酷站演示中心!