WinForm pagination Control Source Download _c# Tutorial

Source: Internet
Author: User

Before are engaged in B/s development, because the company has a relatively large C/s project, in the use of DataGridView, the display data volume is relatively large, so only with paging mode, do not know whether this is correct.

To find a C/s below the paging control, there is nothing good, and b/s under the pagination control, modified into WinForm below.

First create a user control name called pager, drag the BindingNavigator and BindingSource in the control, modify the BindingNavigator, and add some of the necessary controls.

The effect is as follows:

The code implementation is as follows:

namespace Windowsapp.mycontrol {/**////<summary>///affirmation delegate///</summary>///<param name= "E" >
  </param>///<returns></returns> Public delegate int Eventpaginghandler (Eventpagingarg e); /**////<summary>///Paging control rendering///</summary> public partial class Pager:usercontrol {public page
    R () {InitializeComponent ();
    public event Eventpaginghandler Eventpaging;
    /**////<summary>///Displays the number of records per page///</summary> private int _pagesize = 20; /**////<summary>///Displays the number of records per page///</summary> public int PageSize {get {return _pagesiz E
        } set {_pagesize = value;
      Getpagecount ();
    } private int _nmax = 0;
      /**////<summary>///Total record number///</summary> public int Nmax {get {return _nmax;}
        set {_nmax = value;
      Getpagecount ();

    }
    }private int _pagecount = 0;  /**////<summary>///Number of pages = Total Records/Display records per page///</summary> public int PageCount {get {return _pagecount;
    set {_pagecount = value;}
    private int _pagecurrent = 0; /**////<summary>///Current page number///</summary> public int Pagecurrent {get {return _pagecur Rent
    set {_pagecurrent = value;} private void Getpagecount () {if (this. Nmax > 0) {this. PageCount = Convert.ToInt32 (math.ceiling convert.todouble (this. Nmax)/convert.todouble (this.
      PageSize))); } else {this.
      PageCount = 0; /**////<summary>///page Control Data binding method///</summary> public void Bind () {if T His. Eventpaging!= null) {this. Nmax = this. Eventpaging (this.) New Eventpagingarg (this.
      Pagecurrent)); } if (this. Pagecurrent > this. PageCount) {this. Pagecurrent = tHis.
      PageCount; } if (this. PageCount = = 1) {this.
      Pagecurrent = 1; } Lblpagecount.text = this.
      Pagecount.tostring (); This.lblMaxPage.Text = "Total" +this.
      Nmax.tostring () + "record"; This.txtCurrentPage.Text = this.

      Pagecurrent.tostring (); if (this.
        Pagecurrent = = 1) {this.btnPrev.Enabled = false;
      this.btnFirst.Enabled = false;
        else {btnprev.enabled = true;
      Btnfirst.enabled = true; } if (this. Pagecurrent = = this.
        PageCount) {this.btnLast.Enabled = false;
      this.btnNext.Enabled = false;
        else {btnlast.enabled = true;
      Btnnext.enabled = true; } if (this.
        Nmax = = 0) {btnnext.enabled = false;
        btnlast.enabled = false;
        btnfirst.enabled = false;
      btnprev.enabled = false;
   } private void Btnfirst_click (object sender, EventArgs e) {pagecurrent = 1;   This.
    Bind ();
      } private void Btnprev_click (object sender, EventArgs e) {pagecurrent-= 1;
      if (pagecurrent <= 0) {pagecurrent = 1; } this.
    Bind (); } private void Btnnext_click (object sender, EventArgs e) {this.
      Pagecurrent + 1;
      if (Pagecurrent > PageCount) {pagecurrent = PageCount; } this.
    Bind ();
      } private void Btnlast_click (object sender, EventArgs e) {pagecurrent = PageCount; This.
    Bind (); } private void Btngo_click (object sender, EventArgs e) {if (This.txtCurrentPage.Text!= null && TX Tcurrentpage.text!= "") {if (Int32.TryParse (Txtcurrentpage.text, out _pagecurrent)) {th Is.
        Bind (); else {Common.MessageProcess.ShowError ("Input number format error!")
        "); /**////<summary>///Custom event Data base class///</summary> public class eventpagingArg:eventargs {private int _intpageindex;
    Public eventpagingarg (int PageIndex) {_intpageindex = PageIndex;

 }
  }
}

Control functionality is basically implemented.

How do you bind data?

Large number of paging, using stored procedures.

This stored procedure is on the network test, hehe. I posted it, hoping the original author wouldn't hit me with bricks.

ALTER PROCEDURE sp_pagination/**//* *************************************************************** * * Tens of millions of paging stored procedures * * *************************************************************** parameter description: 1.Tables: Table name, view 2.PrimaryKey: Primary keyword 3.Sort: Sort statements without ORDER by example: NewsID desc,orderrows Asc 4.CurrentPage: Current page 5.PageSize: Page size 6.Filter: Filter statements, without where 7.group:group statements , without the group by effect demo: Http://www.cn5135.com/_App/Enterprise/QueryResult.aspx ***************************************** /(@Tables varchar, @PrimaryKey varchar, @Sort varchar = NULL, @CurrentPage int = 1 , @PageSize int = ten, @Fields varchar = ' * ', @Filter varchar (1000) = null, @Group varchar (1000) = null) as/**//* default Sort */IF @Sort is NULL OR @Sort = ' SET @Sort = @PrimaryKey DECLARE @SortTable varchar (1000) DECLARE @SortName varchar (100 0) DECLARE @strSortColumn varchar (1000) DECLARE @operator char (2) DECLARE @type varchar (1000) DECLARE @prec int/**//* Set sort Statement. */IF CHARINDEX (' DESC ', @Sort) >0 BegiN Set @strSortColumn = REPLACE (@Sort, ' DESC ', ') set @operator = ' <= ' end ELSE BEGIN IF CHARINDEX (' ASC ', @Sort) = 0 SE T @strSortColumn = REPLACE (@Sort, ' ASC ', ') SET @operator = ' >= ' End IF CHARINDEX ('. ', @strSortColumn) > 0 BEGIN SE T @SortTable = SUBSTRING (@strSortColumn, 0, CHARINDEX ('. ', @strSortColumn)) SET @SortName = SUBSTRING (@strSortColumn, CHARINDEX ('. ', @strSortColumn) + 1, LEN (@strSortColumn)) End ELSE BEGIN Set @SortTable = @Tables Set @SortName = @strSortCo Lumn End SELECT @type =t.name, @prec =c.prec from sysobjects o join syscolumns C on o.id=c.id join systypes T on C.xusertyp E=t.xusertype WHERE o.name = @SortTable and c.name = @SortName IF CHARINDEX (' char ', @type) > 0 SET @type = @type + ' (' + CAST (@prec as varchar) + ') ' DECLARE @strPageSize varchar (+) DECLARE @strStartRow varchar () DECLARE @strFilter Varc Har (1000) DECLARE @strSimpleFilter varchar (1000) DECLARE @strGroup varchar (1000)/**//* default current page */IF @CurrentPage < 1 SET @CurrentPage = 1/**//* Setting Paging parameters. */Set @strPageSize = CAST (@PageSize as varchar) SET @strStartRow = cast ((@CurrentPage-1) * @PageSize + 1) as Varch AR (500))/**//* filtering and Grouping statements. */IF @Filter is not NULL and @Filter!= ' BEGIN SET @strFilter = ' WHERE ' + @Filter + ' SET @ Strsimplefilter = ' and ' + @Filter + ' end ELSE BEGIN set @strSimpleFilter = ' Set @strFilter = ' End IF @Group is NO  T NULL and @Group!= ' Set @strGroup = ' Group by ' + @Group + ' ELSE SET @strGroup = ' '/**//* Execute query statement */EXEC (' DECLARE @SortColumn ' + @type + ' SET rowcount ' + @strStartRow + ' SELECT @SortColumn = ' + @strSortColumn + ' from ' + @Tables + @strFilter + ' + @strGroup + ' ORDER BY ' + @Sort + ' SET rowcount ' + @strPageSize + ' SELECT ' + @Fields + ' from ' + @Tables + ' WHERE ' + @strSortColumn + @operator + ' @SortColumn ' + @strSimpleFilter + ' + @strGroup + ' ORDER BY ' + @
 Sort + ')

Using the store over Chen, get the data, bind the data to the data control, and provide a pagedata class

/**////<summary>///Data sources provide///</summary> public class Pagedata {private int _pagesize = 10;
    private int _pageindex = 1;
    private int _pagecount = 0;
    private int _totalcount = 0; private string _tablename;//table name private String _queryfieldname = "*";//table field Fieldstr private string _orderstr = Strin G.empty; Sort _sortstr private String _querycondition = String. empty;//the condition of the query RowFilter private string _primarykey = String.
      empty;//primary key/**////<summary>///display pages///</summary> public int PageSize {get

      {return _pagesize;
      } set {_pagesize = value;
        }/**////<summary>///current page///</summary> public int PageIndex {get {
      return _pageindex;
      } set {_pageindex = value;
   }}/**////<summary>///total pages///</summary> public int PageCount {   get {return _pagecount;
      }}/**////<summary>///total record number///</summary> public int TotalCount {get
      {return _totalcount;
      }/**////<summary>///table name, including view///</summary> public string TableName {get
      {return _tablename;
      } set {_tablename = value;
      }/**////<summary>///table field fieldstr///</summary> public string Queryfieldname {
      get {return _queryfieldname;
      } set {_queryfieldname = value;
      }/**////<summary>///sort field///</summary> public string Orderstr {get
      {return _orderstr;
      } set {_orderstr = value; 
      }/**////<summary>///query conditions///</summary> public string Querycondition {get
  {      return _querycondition;
      } set {_querycondition = value;
        }/**////<summary>///primary key///</summary> public string PrimaryKey {get {
      return _primarykey;
      } set {_primarykey = value; } public DataSet querydatatable () {sqlparameter[] parameters = {New SqlParameter ("@Tables" , SqlDbType.VarChar, 255), New SqlParameter ("@PrimaryKey", SqlDbType.VarChar, 255), New Sqlparamet ER ("@Sort", SqlDbType.VarChar, 255), New SqlParameter ("@CurrentPage", SqlDbType.Int), new Sqlparamet
          ER ("@PageSize", SqlDbType.Int), New SqlParameter ("@Fields", SqlDbType.VarChar, 255),
          New SqlParameter ("@Filter", sqldbtype.varchar,1000), New SqlParameter ("@Group", SqlDbType.VarChar, 1000)
      }; Parameters[0].
      Value = _tablename; PARAMETERS[1].
   Value = _primarykey;   PARAMETERS[2].
      Value = _orderstr; PARAMETERS[3].
      Value = PageIndex; PARAMETERS[4].
      Value = PageSize; PARAMETERS[5].
      Value =_queryfieldname; PARAMETERS[6].
      Value = _querycondition; PARAMETERS[7]. Value = string.
      Empty;
      DataSet ds = Dbhelpersql.runprocedure ("sp_pagination", parameters, "DD");
      _totalcount = Gettotalcount ();
        if (_totalcount = = 0) {_pageindex = 0;
      _pagecount = 0;  else {_pagecount = _totalcount% _pagesize = 0? _totalcount/_pagesize: _totalcount/_pagesize
        + 1;

          if (_pageindex > _pagecount) {_pageindex = _pagecount; PARAMETERS[4].

          Value = _pagesize;
        ds = Querydatatable ();
    } return DS;
      public int Gettotalcount () {String strSQL = ' Select count (1) from ' +_tablename; if (_querycondition!= string.
      Empty) {strSQL + = "where" + _querycondition; } return inT.parse (Dbhelpersql.getsingle (strSQL).
    ToString ());

 }
  }

OK, put a DataGridView in the page and drag into the control pager

private void Receiveorderjlform_load (object sender, EventArgs e) {this.pager1.PageCurrent = 1;
    This.pager1.Bind ();
      private int Dgvbind () {WindowsApp.MyControl.PageData pagedata = new WindowsApp.MyControl.PageData ();
      Pagedata.tablename = "T_receiveorder";
      Pagedata.primarykey = "Receiveorderid";
      Pagedata.orderstr = "Receiveorderid desc";
      Pagedata.pageindex = this.pager1.PageCurrent;
      Pagedata.pagesize = this.pager1.PageSize;
      Pagedata.querycondition = _strsql + strwhere.tostring ();

      Pagedata.queryfieldname = "*"; This.pager1.bindingSource.DataSource = Pagedata.querydatatable ().
      Tables[0];
      This.pager1.bindingNavigator.BindingSource = Pager1.bindingsource;
      Dgvreceiveorder.autogeneratecolumns = false;
      Dgvreceiveorder.datasource = This.pager1.bindingSource;
    return pagedata.totalcount; private int pager1_eventpaging (WindowsApp.MyControl.EventPagingArg e) {return DGVBind ();


 }

The effect is as follows

SOURCE Download: Http://xiazai.jb51.net/201609/yuanma/winformPager (jb51.net). rar

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.