WinForm the implementation of a custom paging control

Source: Internet
Author: User

The implementation is a bit ugly but the function is no problem tested

Implementation ideas

Create a user control first

Code implementation

  Public Partial classPagercontrol:usercontrol {Private intRecord =0; /// <summary>        ///Total Record Count/// </summary>         Public intRecord {Get{returnrecord;} Set{record=value;            Initpageinfo (); }        }        Private intPageSize = -; /// <summary>        ///number of bars per page/// </summary>         Public intPageSize {Get{returnPageSize;} Set{pageSize =value;} }        Private intCurrentPage =1; /// <summary>        ///Current Page/// </summary>         Public intCurrentPage {Get{returnCurrentPage;} Set{currentpage =value;} }         Public intPagenum =0; /// <summary>        ///Total Page Number/// </summary>         Public intPagenum {Get            {                if(Record = =0) {Pagenum=0; }                Else                {                    if(Record% PageSize >0) {Pagenum= Record/pagesize +1; }                    Else{pagenum= Record/PageSize; }                }                returnPagenum; }        }        //Defining Delegates         Public Delegate voidBindhandle (Objectsender, EventArgs e); /// <summary>        ///binding Data Source Events/// </summary>         Public EventBindhandle Bindsource;  PublicPagercontrol () {InitializeComponent (); }        /// <summary>        ///Home Page/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidBtnfirst_click (Objectsender, EventArgs e) {            if(Record >0)            {                if(CurrentPage = =1) {MessageBox.Show ("Now is the homepage"); return; }                Else{currentpage=1; if(Bindsource! =NULL) {Bindsource (sender, E);                    Initpageinfo (); }                }            }                   }        Private voidBtnpre_click (Objectsender, EventArgs e) {            if(Record >0)            {                if(CurrentPage = =1) {MessageBox.Show ("Now is the homepage"); return; }                Else{currentpage= CurrentPage-1; if(Bindsource! =NULL) {Bindsource (sender, E);                    Initpageinfo (); }                }            }        }        Private voidBtnnext_click (Objectsender, EventArgs e) {            if(Record >0)            {                if(CurrentPage = =pagenum) {MessageBox.Show ("Now is the last page"); return; }                Else{currentpage= CurrentPage +1; if(Bindsource! =NULL) {Bindsource (sender, E);                    Initpageinfo (); }                }            }        }        Private voidBtnlast_click (Objectsender, EventArgs e) {            if(Record >0)            {                if(CurrentPage = =pagenum) {MessageBox.Show ("Now is the last page"); return; }                Else{currentpage=Pagenum; if(Bindsource! =NULL) {Bindsource (sender, E);                    Initpageinfo (); }                }            }        }        Private voidInitpageinfo () {Lblinfo.text=string. Format ("Total {0} records total {1} pages current section {2}", Record, pagenum,currentpage); Txtpage.text=currentpage.tostring (); }        Private voidBtngo_click (Objectsender, EventArgs e) {            if(Record >0)            {                if(!string. IsNullOrEmpty (txtpage.text) &&! Regex.IsMatch (Txtpage.text,@"^[\d]*$") {MessageBox.Show ("please fill in the correct page number! "); return; }                intpage =Convert.ToInt32 (Txtpage.text); if(page = =0) {page=1; }                if(Page >pagenum) {Page=Pagenum; } currentpage= page; if(Bindsource! =NULL) {Bindsource (sender, E);                Initpageinfo (); }            }        }        Private voidPagercontrol_load (Objectsender, EventArgs e) {            if(Bindsource! =NULL) {Bindsource (sender, E);            Initpageinfo (); }        }    }

Use

Just write the binding method in the form.

 Private voidBind () {stringStart = DtpDate1.Value.ToString ("YYYY-MM-DD"); stringEnd = DtpDate2.Value.ToString ("YYYY-MM-DD"); stringTeam =cbxTeam.SelectedValue.ToString (); stringJieshu =cbxSFJS.SelectedValue.ToString (); intRecord =0; DataTable DT= Eventbiz.getevents (Start, End, team, Jieshu, Pagercontrol1.currentpage, Pagercontrol1.pagesize, outrecord); Pagercontrol1.record=record; Dgvevent.autogeneratecolumns=false; Dgvevent.datasource=dt.        DefaultView; }

Bundle Binding Events

/// <summary>        /// binding Data Sources         /// </summary>        /// <param name= "sender" ></param>        /// <param name= "E" ></param>        Private void Pagercontrol1_bindsource (object  sender, EventArgs e)        {            Bind ();        }

The thing to note is that because the bind method is called in the paging control load event, some form element values are used, so the initialization of the form element item should be placed in the form constructor, not in the form Load event.

WinForm The implementation of the custom paging control

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.