C # dgv paging Function

Source: Internet
Author: User

Complete code

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. data. sqlclient; namespace windowsformsdgvfenye {public partial class form1: FORM {public form1 () {initializecomponent () ;}// source data private datatable dt_sourcedata; // Private int m_currentpage on the current page; // The total number of pages is private. Int zongyeshu; // number of items displayed per page private int m_itemsperpage; // Total number of items private int zongxiangshu; // status private Enum pagestate {init = 0, pre =-1, next = 1} private void form1_load (Object sender, eventargs e) {initdata (); // initialize getdata (); // obtain data} # region initialize private void initdata () {dt_sourcedata = NULL; m_currentpage = 1; // current page m_itemsperpage = 10; // Total number of items per page zongxiangshu = 0; zongyeshu = 0;} # endregion # Regi On refresh data private void getdata () {string strcon = "Server = .; database = HSPB; uid = sa; Pwd = 123; "; sqlconnection conn = new sqlconnection (strcon); Conn. open (); string strsql = "select Xingming, xingbie, xuexing, chushengnianyue from Hangzhou"; sqlcommand cmd = new sqlcommand (strsql, Conn); sqldataadapter da = new sqldataadapter (CMD ); // sqldataadapter da = new sqldataadapter (strsql, Conn); // define d Ataadapter dataset DS = new dataset (); DA. fill (DS); // datagridview1.datasource = Ds. tables [0]; setsourcedata (Ds. tables [0]);} # endregion // set the public void setsourcedata (Object sourcedata) {dt_sourcedata = (datatable) sourcedata; ///// obtain the total number of items zongxiangshu = dt_sourcedata.rows.count; ///// total number of page zongyeshu = (zongxiangshu/m_itemsperpage) + (zongxiangshu % m_itemsperpage! = 0 )? 1: 0); // the total number of pages updateuidata (pagestate. init);} // update the interface information private void updateuidata (pagestate) {setdgvsourcedata (pagestate); // initialize the data m_currentpage + = (INT) pagestate; updatebuttonstate (); // Update button status setcurroftotalpage (); // set [current page/total page number]} // set the dgv data source <display data> private void setdgvsourcedata (pagestate) {datatable viewdata = dt_sourcedata.clone (); // copy the organizational structure int nbeginindex = 0; int nendindex = 0; Getcurrentpageindex (pagestate, out nbeginindex, out nendindex); For (; nbeginindex <nendindex; nbeginindex ++) {viewdata. importrow (parts [nbeginindex]);} datagridview1.datasource = viewdata;} // set the current page number/total page number private void setcurroftotalpage () {label1.text = m_currentpage.tostring () + "/" + zongyeshu. tostring () + "page";} // obtain the minimum subscript and maximum subscript private void getcurrentpageindex (pagestate Pagestate, out int beginindex, out int endindex) {If (pagestate. init = pagestate) // initialize {beginindex = 0; endindex = m_itemsperpage;} else if (pagestate. pre = pagestate) // Previous Page {endindex = m_currentpage * m_itemsperpage; beginindex = (m_currentpage-1) * m_itemsperpage;} else // next page {beginindex = m_currentpage * m_itemsperpage; endindex = (m_currentpage + 1) * m_itemsperpage;} // If (be Ginindex <0) {beginindex = 0;} If (endindex> zongxiangshu) {endindex = zongxiangshu;} // The Private void tsb_prepage_click (Object sender, eventargs e) button on the previous page) {updateuidata (pagestate. PRE);} // the next page button private void tsb_nextpage_click (Object sender, eventargs e) {updateuidata (pagestate. next);} // Update button status private void updatebuttonstate () {// process the previous page button if (1 = m_currentpage) {If (tsb_prepage.enabled) // Shield {tsb_prepage.enabled = false ;}} else if (! Tsb_prepage.enabled) {tsb_prepage.enabled = true;} // process the next page button if (zongyeshu = m_currentpage) {If (response) {tsb_nextpage.enabled = false;} else if (! Tsb_nextpage.enabled) {tsb_nextpage.enabled = true ;}}}}

 

C # dgv paging Function

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.