程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> DataGridView 的分頁處理

DataGridView 的分頁處理

編輯:.NET實例教程


只是解決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 &
nbsp;       /// <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"&g
t;</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                &nbs
p;        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
http://www.cnblogs.com/azhai/archive/2006/09/18/507624.Html

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved