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