Asp. NET Tip: DataGridView page-handling

Source: Internet
Author: User
asp.net|datagrid| Pagination | tips

Just solve the DataGridView refresh slow way.
If you want to consider efficiency, or the combination of pagination of the "stored procedures" is better, just a flexible.

1 #region Data processing
2///<summary>
3///get the opening data
4///</summary>
5 private void Getfilldateset ()
6 {
7 Try
8 {
9//per page display number
Ten pageSize = 10;
11//Maximum number of records
Maxrec = DtSource.Rows.Count;
13//Total number of pages
PageCount = (maxrec/pagesize);
15//Pick remainder
if ((maxrec% pageSize) > 0)
17 {
pagecount++;
19}
20//Default first page
CurrentPage = 1;
RECNO = 0;
LoadPage ();
24}
Catch
26 {
27}
28}
29
30
///<summary>
32///to determine whether the data has been loaded
///</summary>
///<returns></returns>
private bool CheckFillButton ()
36 {
if (pageSize = 0) return false;
/else return true;
39}
40
///<summary>
42///Data from DataTable
///</summary>
The private void LoadPage ()
45 {
Startrec int;
Endrec int;
The DataTable dttemp;
49
50
Wuyi dttemp = Dtsource.clone ();
if (currentpage = = PageCount) Endrec = Maxrec;
or else Endrec = pageSize * currentpage;
Startrec = Recno;
(int i = Startrec i < Endrec; i++)
56 {
Dttemp.importrow (Dtsource.rows[i]);
recno++;
59}
This.dataGridView1.DataSource = dttemp;
61}
62
///<summary>
64///Paging Event (take the tag value of each key)
///</summary>
///<param name= "Sender" ></param>
///<param name= "E" ></param>
private void Changepage (object sender, EventArgs e)
69 {
if (! CheckFillButton ()) return;
The int myint = Convert.ToInt16 ((string) (sender as Button). TAG);
Switch (Myint)
73 {
Case 1:
if (currentpage = 1)
76 {
MessageBox.Show ("The Frist page now!");
The return;
79
80}
Bayi currentpage = 1;
RECNO = 0;
LoadPage ();
The break;
Case 2:
currentpage++;
if (CurrentPage > PageCount)
88 {
CurrentPage = PageCount;
if (recno = = Maxrec)
91 {
MessageBox.Show ("The last page now!");
The return of the return;
94}
Or else
RECNO = PageSize * (currentpage + 1);
97}
LoadPage ();
The break;
Case 3:
An if (currentpage = = PageCount)
102 RECNO = PageSize * (currentPage-2);
The currentpage--;
(CurrentPage < 1)
105 {
The MessageBox.Show ("The Frist page now!");
CurrentPage = 1;
108 return;
109}
It else
RECNO = PageSize * (currentPage-1);
112
113 LoadPage ();
114 break;
115
116 Case 4:
117 if (! CheckFillButton ()) return;
118 if (recno = = Maxrec)
119 {
MessageBox.Show ("The last page now!");
121 return;
122}
123 currentpage = PageCount;
124 Recno = PageSize * (currentPage-1);
LoadPage ();
126 break;
127}
128
129}
130
131 #endregion



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.