Implementation of the pagination function of the gridview

Source: Internet
Author: User

When many records are displayed in the gridview, you can use the pagination function of the gridview to display these records by page. If the gridview is directly bound to the database, it is easy to: Tick the "start page.

If it is implemented using code, you need to do this:

1. Enable the pagination allowpaging attribute to true;

2. Set Some attributes in pagersetting in the property bar of the gridview to define the pagination style;

3. Bind the data to display the data on the gridview;

4. Trigger related events to display data on pages.

 

Some code:

1. query and bind data

1 /// <summary> 2 // query data for Data Binding 3 /// </Summary> 4 private void binddata () 5 {6 // deploy data to 7 string constr = "database information"; 8 string sqlstr = "SQL statement"; 9 sqlconnection con = new sqlconnection (constr ); 10 sqldataadapter ad = new sqldataadapter (sqlstr, con); 11 dataset DS = new dataset (); 12 ad. fill (DS); 13 gridview1.datasource = Ds; 14 gridview1.databind (); 15}
View code

2. event handling

1 /// <summary> 2 // paging event 3 /// </Summary> 4 protected void gridview1_pageindexchanging (Object sender, gridviewpageeventargs e) 5 {6 gridview1.pageindex = E. newpageindex; 7 // rebind data 8 binddata (); 9}
View code

3. Add pagination

1 /// <summary> 2 /// Add the score page to display 3 /// </Summary> 4 protected void gridview1_databound (Object sender, eventargs E) 5 {6 // Add the page to display 7 gridviewrow bottompagerrow = gridview1.bottompagerrow; 8 label bottomlabel = new label (); 9 bottomlabel. TEXT = "Current page: (" + (gridview1.pageindex + 1) + "/" + gridview1.pagecount + ")"; 10 bottompagerrow. cells [0]. controls. add (bottomlabel); 11}
View code

 

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.