大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> DataGrid连接Access的快速分页法(5)——实现快速分页

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

时间: 2021-07-31 作者:daque

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 } }}

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.