首页 > 编程 > .NET > 正文

ASP.NET技巧:DataGridView 的分页处理

2024-07-10 13:09:34
字体:
来源:转载
供稿:网友

只是解决datagridview刷新慢的办法。
如果要考虑效率,还是结合分页的"存储过程"比较好,只是变通了一下。

  1        #region 数据处理
  2         /// <summary>
  3         /// 得到期初数据
  4         /// </summary>
  5         private void getfilldateset()
  6         {
  7             try
  8             {
  9                 //每页显示数
 10                 pagesize = 10;
 11                 //得到最大记录数
 12                 maxrec = dtsource.rows.count;
 13                 //共有多少页
 14                 pagecount = (maxrec / pagesize);
 15                 //取余数
 16                 if ((maxrec % pagesize) > 0)
 17                 {
 18                     pagecount++;
 19                 }
 20                 //默认第一页
 21                 currentpage = 1;
 22                 recno = 0;
 23                 loadpage();
 24             }
 25             catch
 26             {
 27             }
 28         }
 29
 30
 31         /// <summary>
 32         /// 判断是否数据已经加载
 33         /// </summary>
 34         /// <returns></returns>
 35         private bool checkfillbutton()
 36         {
 37             if (pagesize == 0) return false;
 38             else return true;
 39         }
 40
 41        /// <summary>
 42         /// 取datatable的数据
 43        /// </summary>
 44         private void loadpage()
 45         {
 46             int startrec;
 47             int endrec;
 48             datatable dttemp;
 49  
 50
 51             dttemp = dtsource.clone();
 52             if (currentpage == pagecount) endrec = maxrec;
 53             else endrec = pagesize * currentpage;
 54             startrec = recno;
 55             for (int i = startrec; i < endrec; i++)
 56             {
 57                 dttemp.importrow(dtsource.rows[i]);
 58                 recno++;
 59             }
 60             this.datagridview1.datasource = dttemp;
 61         }
 62
 63        /// <summary>
 64        /// 分页事件(取各个按键的tag值)
 65        /// </summary>
 66        /// <param name="sender"></param>
 67        /// <param name="e"></param>
 68         private void changepage(object sender, eventargs e)
 69         {
 70             if (!checkfillbutton()) return;
 71             int myint = convert.toint16((string)(sender as button).tag);
 72             switch (myint)
 73             {
 74                 case 1:
 75                     if (currentpage == 1)
 76                     {
 77                         messagebox.show("the frist page now!");
 78                         return;
 79
 80                     }
 81                     currentpage = 1;
 82                     recno = 0;
 83                     loadpage();
 84                     break;
 85                 case 2:
 86                     currentpage++;
 87                     if (currentpage > pagecount)
 88                     {
 89                         currentpage = pagecount;
 90                         if (recno == maxrec)
 91                         {
 92                             messagebox.show("the last page now!");
 93                             return;
 94                         }
 95                         else
 96                             recno = pagesize * (currentpage + 1);
 97                     }
 98                     loadpage();
 99                     break;
100                 case 3:
101                     if (currentpage == pagecount)
102                         recno = pagesize * (currentpage - 2);
103                     currentpage--;
104                     if (currentpage < 1)
105                     {
106                         messagebox.show("the frist page now!");
107                         currentpage = 1;
108                         return;
109                     }
110                     else
111                         recno = pagesize * (currentpage - 1);
112
113                     loadpage();
114                     break;
115
116                 case 4:
117                     if (!checkfillbutton()) return;
118                     if (recno == maxrec)
119                     {
120                         messagebox.show("the last page now!");
121                         return;
122                     }
123                     currentpage = pagecount;
124                     recno = pagesize * (currentpage - 1);
125                     loadpage();
126                     break;
127             }
128
129         }
130
131 #endregion



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表