• Winform 分页


    1.图列展示

    2.分页控件代码

    Paging.Designer.cs

     partial class Paging
        {
            /// <summary> 
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary> 
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region 组件设计器生成的代码
    
            /// <summary> 
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Paging));
                this.bindingNavigator = new System.Windows.Forms.BindingNavigator(this.components);
                this.btnFirst = new System.Windows.Forms.ToolStripButton();
                this.btnPrev = new System.Windows.Forms.ToolStripButton();
                this.btnNext = new System.Windows.Forms.ToolStripButton();
                this.btnLast = new System.Windows.Forms.ToolStripButton();
                this.toolStripLabel6 = new System.Windows.Forms.ToolStripLabel();
                this.toolStripLabel4 = new System.Windows.Forms.ToolStripLabel();
                this.txtCurrentPage = new System.Windows.Forms.ToolStripTextBox();
                this.toolStripLabel1 = new System.Windows.Forms.ToolStripLabel();
                this.btnGo = new System.Windows.Forms.ToolStripButton();
                this.toolStripLabel5 = new System.Windows.Forms.ToolStripLabel();
                this.toolStripLabel2 = new System.Windows.Forms.ToolStripLabel();
                this.lblPageCount = new System.Windows.Forms.ToolStripLabel();
                this.toolStripLabel3 = new System.Windows.Forms.ToolStripLabel();
                this.lblMaxPage = new System.Windows.Forms.ToolStripLabel();
                ((System.ComponentModel.ISupportInitialize)(this.bindingNavigator)).BeginInit();
                this.bindingNavigator.SuspendLayout();
                this.SuspendLayout();
                // 
                // bindingNavigator
                // 
                this.bindingNavigator.AddNewItem = null;
                this.bindingNavigator.CountItem = null;
                this.bindingNavigator.DeleteItem = null;
                this.bindingNavigator.Dock = System.Windows.Forms.DockStyle.Bottom;
                this.bindingNavigator.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.btnFirst,
                this.btnPrev,
                this.btnNext,
                this.btnLast,
                this.toolStripLabel6,
                this.toolStripLabel4,
                this.txtCurrentPage,
                this.toolStripLabel1,
                this.btnGo,
                this.toolStripLabel5,
                this.toolStripLabel2,
                this.lblPageCount,
                this.toolStripLabel3,
                this.lblMaxPage});
                this.bindingNavigator.Location = new System.Drawing.Point(0, 3);
                this.bindingNavigator.MoveFirstItem = null;
                this.bindingNavigator.MoveLastItem = null;
                this.bindingNavigator.MoveNextItem = null;
                this.bindingNavigator.MovePreviousItem = null;
                this.bindingNavigator.Name = "bindingNavigator";
                this.bindingNavigator.PositionItem = null;
                this.bindingNavigator.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
                this.bindingNavigator.Size = new System.Drawing.Size(713, 25);
                this.bindingNavigator.TabIndex = 2;
                this.bindingNavigator.Text = "bindingNavigator1";
                // 
                // btnFirst
                // 
                this.btnFirst.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
                this.btnFirst.Image = ((System.Drawing.Image)(resources.GetObject("btnFirst.Image")));
                this.btnFirst.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.btnFirst.Name = "btnFirst";
                this.btnFirst.Size = new System.Drawing.Size(36, 22);
                this.btnFirst.Text = "首页";
                this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
                // 
                // btnPrev
                // 
                this.btnPrev.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
                this.btnPrev.Image = ((System.Drawing.Image)(resources.GetObject("btnPrev.Image")));
                this.btnPrev.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.btnPrev.Name = "btnPrev";
                this.btnPrev.Size = new System.Drawing.Size(36, 22);
                this.btnPrev.Text = "上页";
                this.btnPrev.Click += new System.EventHandler(this.btnPrev_Click);
                // 
                // btnNext
                // 
                this.btnNext.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
                this.btnNext.Image = ((System.Drawing.Image)(resources.GetObject("btnNext.Image")));
                this.btnNext.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.btnNext.Name = "btnNext";
                this.btnNext.Size = new System.Drawing.Size(36, 22);
                this.btnNext.Text = "下页";
                this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
                // 
                // btnLast
                // 
                this.btnLast.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
                this.btnLast.Image = ((System.Drawing.Image)(resources.GetObject("btnLast.Image")));
                this.btnLast.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.btnLast.Name = "btnLast";
                this.btnLast.Size = new System.Drawing.Size(36, 22);
                this.btnLast.Text = "未页";
                this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
                // 
                // toolStripLabel6
                // 
                this.toolStripLabel6.Name = "toolStripLabel6";
                this.toolStripLabel6.Size = new System.Drawing.Size(28, 22);
                this.toolStripLabel6.Text = "     ";
                // 
                // toolStripLabel4
                // 
                this.toolStripLabel4.Name = "toolStripLabel4";
                this.toolStripLabel4.Size = new System.Drawing.Size(20, 22);
                this.toolStripLabel4.Text = "";
                // 
                // txtCurrentPage
                // 
                this.txtCurrentPage.Name = "txtCurrentPage";
                this.txtCurrentPage.Size = new System.Drawing.Size(40, 25);
                // 
                // toolStripLabel1
                // 
                this.toolStripLabel1.Name = "toolStripLabel1";
                this.toolStripLabel1.Size = new System.Drawing.Size(20, 22);
                this.toolStripLabel1.Text = "";
                // 
                // btnGo
                // 
                this.btnGo.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
                this.btnGo.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold);
                this.btnGo.Image = ((System.Drawing.Image)(resources.GetObject("btnGo.Image")));
                this.btnGo.ImageTransparentColor = System.Drawing.Color.Magenta;
                this.btnGo.Name = "btnGo";
                this.btnGo.Size = new System.Drawing.Size(23, 22);
                this.btnGo.Text = "GO";
                this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
                // 
                // toolStripLabel5
                // 
                this.toolStripLabel5.Name = "toolStripLabel5";
                this.toolStripLabel5.Size = new System.Drawing.Size(28, 22);
                this.toolStripLabel5.Text = "     ";
                // 
                // toolStripLabel2
                // 
                this.toolStripLabel2.Name = "toolStripLabel2";
                this.toolStripLabel2.Size = new System.Drawing.Size(20, 22);
                this.toolStripLabel2.Text = "";
                // 
                // lblPageCount
                // 
                this.lblPageCount.Name = "lblPageCount";
                this.lblPageCount.Size = new System.Drawing.Size(85, 22);
                this.lblPageCount.Text = "lblPageCount";
                // 
                // toolStripLabel3
                // 
                this.toolStripLabel3.Name = "toolStripLabel3";
                this.toolStripLabel3.Size = new System.Drawing.Size(20, 22);
                this.toolStripLabel3.Text = "";
                // 
                // lblMaxPage
                // 
                this.lblMaxPage.Name = "lblMaxPage";
                this.lblMaxPage.Size = new System.Drawing.Size(76, 22);
                this.lblMaxPage.Text = "lblMaxPage";
                // 
                // Paging
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.Controls.Add(this.bindingNavigator);
                this.Name = "Paging";
                this.Size = new System.Drawing.Size(713, 28);
                ((System.ComponentModel.ISupportInitialize)(this.bindingNavigator)).EndInit();
                this.bindingNavigator.ResumeLayout(false);
                this.bindingNavigator.PerformLayout();
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            public System.Windows.Forms.BindingNavigator bindingNavigator;
            private System.Windows.Forms.ToolStripButton btnFirst;
            private System.Windows.Forms.ToolStripButton btnPrev;
            private System.Windows.Forms.ToolStripButton btnNext;
            private System.Windows.Forms.ToolStripButton btnLast;
            private System.Windows.Forms.ToolStripLabel toolStripLabel6;
            private System.Windows.Forms.ToolStripLabel toolStripLabel4;
            private System.Windows.Forms.ToolStripTextBox txtCurrentPage;
            private System.Windows.Forms.ToolStripLabel toolStripLabel1;
            private System.Windows.Forms.ToolStripButton btnGo;
            private System.Windows.Forms.ToolStripLabel toolStripLabel5;
            private System.Windows.Forms.ToolStripLabel toolStripLabel2;
            private System.Windows.Forms.ToolStripLabel lblPageCount;
            private System.Windows.Forms.ToolStripLabel toolStripLabel3;
            private System.Windows.Forms.ToolStripLabel lblMaxPage;
        }

    Paging.cs

      /// <summary>
            /// 申明委托
            /// </summary>
            /// <param name="e"></param>
            /// <returns></returns>
            public delegate int EventPagingHandler(EventPagingArg e);
            public partial class Paging : UserControl
            {
                public Paging()
                {
                    InitializeComponent();
                }
                public event EventPagingHandler EventPaging;
                /// <summary>
                /// 每页显示记录数
                /// </summary>
                private int _pageSize = 20;
                /// <summary>
                /// 每页显示记录数
                /// </summary>
                public int PageSize
                {
                    get { return _pageSize; }
                    set
                    {
                        _pageSize = value;
                        GetPageCount();
                    }
                }
    
                private int _nMax = 0;
                /// <summary>
                /// 总记录数
                /// </summary>
                public int NMax
                {
                    get { return _nMax; }
                    set
                    {
                        _nMax = value;
                        GetPageCount();
                    }
                }
    
                private int _pageCount = 0;
                /// <summary>
                /// 页数=总记录数/每页显示记录数
                /// </summary>
                public int PageCount
                {
                    get { return _pageCount; }
                    set { _pageCount = value; }
                }
    
                private int _pageCurrent = 0;
                /// <summary>
                /// 当前页号
                /// </summary>
                public int PageCurrent
                {
                    get { return _pageCurrent; }
                    set { _pageCurrent = value; }
                }
                private DataTable _dataSource;
                /// <summary>
                ///  数据源
                /// </summary>
                public DataTable DataSource
                {
                    get { return _dataSource; }
                    set
                    {
                        _dataSource = value;
                    }
                }
                public BindingNavigator ToolBar
                {
                    get { return this.bindingNavigator; }
                }
                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>
                /// 翻页控件数据绑定的方法
                /// </summary>
                public void Bind()
                {
                    if (this.EventPaging != null)
                    {
                        this.NMax = this.EventPaging(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 = "" + this.NMax.ToString() + "条记录";
                    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 && txtCurrentPage.Text != "")
                    {
                        if (Int32.TryParse(txtCurrentPage.Text, out _pageCurrent))
                        {
                            this.Bind();
                        }
                        else
                        {
                            MessageBox.Show("输入数字格式错误!");
                        }
                    }
                }
    
                private void txtCurrentPage_KeyDown(object sender, KeyEventArgs e)
                {
                    if (e.KeyCode == Keys.Enter)
                    {
                        this.Bind();
                    }
                }
    
            }
            /// <summary>
            /// 自定义事件数据基类
            /// </summary>
            public class EventPagingArg : EventArgs
            {
                private int _intPageIndex;
                public EventPagingArg(int PageIndex)
                {
                    _intPageIndex = PageIndex;
                }
            }

    使用:
    PagingHelper

     class PagingHelper
        {
    
                private int _PageSize = 10;
                private int _PageIndex = 1;
                private int _TotalCount = 0;
                private string _TableName;//表名
                private string _QueryFieldName = "*";//表字段FieldStr
                private string _OrderStr = string.Empty; //排序_SortStr
                private string _QueryCondition = string.Empty;//查询的条件 RowFilter
                private string _PrimaryKey = string.Empty;//主键
              
                /// <summary>
                /// 显示页数
                /// </summary>
                public int PageSize
                {
                    get
                    {
                        return _PageSize;
    
                    }
                    set
                    {
                        _PageSize = value;
                    }
                }
                /// <summary>
                /// 当前页
                /// </summary>
                public int PageIndex
                {
                    get
                    {
                        return _PageIndex;
                    }
                    set
                    {
                        _PageIndex = value;
                    }
                }
              
                /// <summary>
                /// 总记录数
                /// </summary>
                public int TotalCount
                {
                    get
                    {
                        return _TotalCount;
                    }
                }
                /// <summary>
                /// 表名,包括视图
                /// </summary>
                public string TableName
                {
                    get
                    {
                        return _TableName;
                    }
                    set
                    {
                        _TableName = value;
                    }
                }
                /// <summary>
                /// 表字段FieldStr
                /// </summary>
                public string QueryFieldName
                {
                    get
                    {
                        return _QueryFieldName;
                    }
                    set
                    {
                        _QueryFieldName = value;
                    }
                }
                /// <summary>
                /// 排序字段
                /// </summary>
                public string OrderStr
                {
                    get
                    {
                        return _OrderStr;
                    }
                    set
                    {
                        _OrderStr = value;
                    }
                }
                public  DataTable QueryDataTable()
                {
                   //这里调用存储过程实现数据读取
                   //展示就用本地测试数据
                    DataTable dt = new DataTable();
                    dt.Columns.Add("ID", typeof(int));
                    dt.Columns.Add("Name", typeof(string));
                    _TotalCount = 101;
                  
                        int index = PageIndex == 1 ? 1 : PageIndex * PageSize;
                        for (int i = index; i < index+PageSize; i++)
                        {
                            dt.Rows.Add(i, "Name" + i.ToString());
                        }
                      dt.AcceptChanges();
                    
                   
                    return dt;
                }
        }

    测试页面调用:

     private void Form1_Load(object sender, EventArgs e)
            {
                this.paging1.PageCurrent = 1;
                this.paging1.Bind();
            }
          
            private int paging1_EventPaging(EventPagingArg e)
            {
               return  Bind(this.paging1.PageCurrent);
            }
            private int Bind(int pagecurrent)
            {
                PagingHelper phelper = new PagingHelper();
                phelper.PageIndex = pagecurrent;
                paging1.PageSize = 20;
                DataTable dt = phelper.QueryDataTable();
                this.dataGridView1.DataSource = dt.DefaultView;
                return phelper.TotalCount;
            }
  • 相关阅读:
    [Json.net]忽略不需要的字段
    [c#]exchange回复,全部回复,转发所遇到的问题
    [c#]获取exchange中的图片
    [c#基础]AutoResetEvent
    [C#基础]c#中的BeginInvoke和EndEndInvoke
    [CentOs7]安装mysql
    [CentOS7]安装mysql遇到的问题
    [CentOs7]图形界面
    [CentOS]添加删除用户
    在虚机中安装CentOS
  • 原文地址:https://www.cnblogs.com/linsu/p/4751025.html
Copyright © 2020-2023  润新知