• 学习:WinForm分页控件(转)


     分页控件结合DataGridView


    SelectPaging.JPG

    分页用户控件,使用效果如上图,可以用在查询窗体,代码如下:

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Drawing;

    using System.Data;

    using System.Text;

    using System.Windows.Forms;

     

    namespace Kimbanx.SecurityDiskSystem.Controls

    {

        public partial class SelectPaging : UserControl

        {/**//// <summary>

            /// 每页显示记录数

            /// </summary>

            private int _pageSize = 20;

            /**//// <summary>

            /// 每页显示记录数

            /// </summary>

            public int PageSize

            {

                get { return _pageSize; }

                set { _pageSize = value; }

            }/**//// <summary>

            /// 总记录数

            /// </summary>

            private int _nMax = 0;

            /**//// <summary>

            /// 总记录数

            /// </summary>

            public int NMax

            {

                get { return _nMax; }

                set { _nMax = value; }

            }/**//// <summary>

            /// 页数=总记录数/每页显示记录数

            /// </summary>

            private int _pageCount = 0;

            /**//// <summary>

            /// 页数=总记录数/每页显示记录数

            /// </summary>

            public int PageCount

            {

                get { return _pageCount; }

                set { _pageCount = value; }

            }/**//// <summary>

            /// 当前页号

            /// </summary>

            private int _pageCurrent = 0;

            /**//// <summary>

            /// 当前页号

            /// </summary>

            public int PageCurrent

            {

                get { return _pageCurrent; }

                set { _pageCurrent = value; }

            }/**//// <summary>

            /// 当前记录行

            /// </summary>

            private int _nCurrent = 0;

            /**//// <summary>

            /// 当前记录行

            /// </summary>

            public int NCurrent

            {

                get { return _nCurrent; }

                set { _nCurrent = value; }

            }

            /**//// <summary>

            /// 设置数据源

            /// </summary>

            private DataTable dataSource = null;

            /**//// <summary>

            /// 设置数据源

            /// </summary>

            public DataTable DataSource

            {

                get { return dataSource; }

                set { dataSource = value; }

            }

            //private DataSet ds = null;

     

            public void InitDataSet(DataTable dt)

            {

                this.dataSource = dt;

                this._nMax = this.dataSource.Rows.Count;

                //计算总页数

                this._pageCount = _nMax / _pageSize;

                //

                if ((_nMax % _pageSize) > 0)

                    _pageCount++;

                _pageCurrent = 1;

                _nCurrent = 0;

                FillData();

            }

            /**//// <summary>

            /// 默认构造函数

            /// </summary>

            public SelectPaging()

            {

                InitializeComponent();

                lblMsg.Enabled = false;

     

            }

            /**//// <summary>

            /// 填充数据

            /// </summary>

            private void FillData()

            {

                //当前页面开始记录行

                int nStartPos = 0;

                //当前页面结束记录行

                int nEndPos = 0;

                DataTable dtTemp = this.dataSource.Clone();

                if (this._pageCurrent == this._pageCount)

                    nEndPos = _nMax;

                else

                    nEndPos = this._pageCurrent * this._pageSize;

     

                nStartPos = this._nCurrent;

     

                lblMaxPage.Text = this._pageCount.ToString();

                txtCurrentPage.Text = this._pageCurrent.ToString();

     

                for (int i = nStartPos; i < nEndPos; i++)

                {

                    dtTemp.ImportRow(this.dataSource.Rows[i]);

                    _nCurrent++;

                }

                bdSource.DataSource = dtTemp;

                bdNav.BindingSource = bdSource;

                dataGV.DataSource = bdSource;

     

            }

     

            private void bdNav_ItemClicked(object sender, ToolStripItemClickedEventArgs e)

            {

                switch (e.ClickedItem.Text)

                {

                    case "移到上一条记录":

                        //FillData();

                        break;

                    case "移到下一条记录":

                        //FillData();

                        break;

                    case "上一页":

                      

                        if (this._pageCurrent == 1)

                        {

                            lblMsg.Text = "已经是第一页,请点击下一页查看!";

                            //btnPrePage.Enabled = false;

                            return;

                        }

                        else

                       

                            _pageCurrent--;

                            this._nCurrent = this._pageSize * (_pageCurrent - 1);

                            FillData();

                        }

     

                        break;

                    case "下一页":

                       

                        if (this._pageCurrent == _pageCount)

                        {

                            lblMsg.Text = "已经是最后一页,请点击上一页查看!";

                            return;

                        }

                        else

                        {

                            _pageCurrent++;

                            this._nCurrent = this._pageSize * (_pageCurrent - 1); FillData();

                        }

                      

                        break;

                }

               

            }

        }

    }


     bdNav是bandingnavigator分页控件
     dataGV是网格控件datagridview控件
     bdsource是绑定源控件bandingsource


    文章来源:http://www.cnblogs.com/virusswb/archive/2008/11/05/1239783.html




  • 相关阅读:
    洛谷P1455 搭配购买
    洛谷1341 无序字母对
    打击犯罪
    Cheese
    [noip2002] 产生数
    分治算法-----二分求最大最小
    yl 练习
    cj 练习
    雅礼2018-03-19洛谷作业 2
    雅礼2018-03-19洛谷作业
  • 原文地址:https://www.cnblogs.com/LeimOO/p/1579831.html
Copyright © 2020-2023  润新知