winform dateGrid实现分页(升级版),增加每页显示的行数

一、分页现实效果:
在这里插入图片描述二.功能实现
1.新建用户控件
在这里插入图片描述
2.在用户控件中拉如下图控件(并注册对应的事件)
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Glory.Common.UI
{
    public partial class UGridPage : UserControl
    {
        public delegate void JumpToPageIndex(int pageIndex, int rowsPerPage);
        public event JumpToPageIndex PageJumpTo;

        private bool _jumpToPageFocused;
        /// <summary>
        /// 跳转第几页有没有获得焦点
        /// </summary>
        public bool JumpToPageFocused
        {
            get { return _jumpToPageFocused; }
        }

        private int _pageCount;
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount {
            get { return _pageCount; }
        }

        private int _rowCount;
        /// <summary>
        /// 总行数
        /// </summary>
        public int RowCount
        {
            get { return _rowCount; }
        }

        public void SetRowCount(int rowCount)
        {
            if (_rowCount < 0)
            {
                _rowCount = 0;
                lbDesc.Text = "";
            }
            else
            {
                _rowCount = rowCount;
                _pageCount = (int)Math.Ceiling((double)_rowCount / _rowsPerPage);
                lbDesc.Text = string.Format("共{0}页,共{1}条数据", _pageCount, _rowCount);
                txtRowsPerPage.Text = _rowsPerPage.ToString();
                setCurrentPage(1);
            }
        }

        private int _rowsPerPage = 10;
        /// <summary>
        /// 每页行数
        /// </summary>
        public int RowsPerPage
        {
            get { return _rowsPerPage; }
            set
            {
                if (value > 0)
                {
                    _rowsPerPage = value;
                }
            }
        }

        private int _currentPage;
        /// <summary>
        /// 当前第几页
        /// </summary>
        public int CurrentPage
        {
            get { return _currentPage; }
        }

        public UGridPage()
        {
            InitializeComponent();
        }

        private void UGridPage_Load(object sender, EventArgs e)
        {
            llPgHm.Text = "Page Home";
            llPgUp.Text = "Page Up";
            llPgDn.Text = "Page Down";
            llPgEd.Text = "Page End";
            
            lbDesc.Text = "";

            txtCurrentPage.GotFocus += Txt_GotFocus;
            txtCurrentPage.LostFocus += Txt_LostFocus;
            txtRowsPerPage.GotFocus += Txt_GotFocus;
            txtRowsPerPage.LostFocus += Txt_LostFocus;
        }

        private void Txt_LostFocus(object sender, EventArgs e)
        {
            _jumpToPageFocused = false;
        }

        private void Txt_GotFocus(object sender, EventArgs e)
        {
            _jumpToPageFocused = true;
        }
        /// <summary>
        /// 前面还有没有页
        /// </summary>
        /// <param name="havePage"></param>
        private void setHavePrePage(bool havePage)
        {
            llPgHm.Enabled = havePage;
            llPgUp.Enabled = havePage;
        }
        /// <summary>
        /// 后面还有没有页
        /// </summary>
        /// <param name="havePage"></param>
        private void setHavePostPage(bool havePage)
        {
            llPgEd.Enabled = havePage;
            llPgDn.Enabled = havePage;
        }

        private void setCurrentPage(int currentPage)
        {
            _currentPage = currentPage;
            txtCurrentPage.Text = _currentPage.ToString();
            if (_currentPage==1)
            {
                setHavePrePage(false);
            }
            else
            {
                setHavePrePage(true);
            }
            if (_currentPage>=_pageCount)
            {
                setHavePostPage(false);
            }
            else
            {
                setHavePostPage(true);
            }
        }

        /// <summary>
        /// 第一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void llPgHm_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            setCurrentPage(1);
            if (PageJumpTo != null)
            {
                PageJumpTo(_currentPage, _rowsPerPage);
            }
        }
        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void llPgUp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            setCurrentPage(_currentPage-1);
            if (PageJumpTo != null)
            {
                PageJumpTo(_currentPage, _rowsPerPage);
            }
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void llPgDn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            setCurrentPage(_currentPage + 1);
            if (PageJumpTo != null)
            {
                PageJumpTo(_currentPage, _rowsPerPage);
            }
        }
        /// <summary>
        /// 最后页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void llPgEd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            setCurrentPage(_pageCount);
            if (PageJumpTo != null)
            {
                PageJumpTo(_currentPage, _rowsPerPage);
            }
        }
        /// <summary>
        /// 页跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtCurrentPage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                int jumpPageIndex;
                if (!int.TryParse(txtCurrentPage.Text, out jumpPageIndex))
                {
                    txtCurrentPage.Text = _currentPage.ToString();
                    return;
                }
                if (jumpPageIndex <= 0)
                {
                    txtCurrentPage.Text = _currentPage.ToString();
                    return;
                }
                if (jumpPageIndex>_pageCount)
                {
                    txtCurrentPage.Text = _currentPage.ToString();
                    return;
                }
                setCurrentPage(jumpPageIndex);
                if (PageJumpTo != null)
                {
                    PageJumpTo(_currentPage, _rowsPerPage);
                }
            }
        }

        private void txtRowsPerPage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                int rowsPerPage;
                if (!int.TryParse(txtRowsPerPage.Text, out rowsPerPage))
                {
                    txtRowsPerPage.Text = _rowsPerPage.ToString();
                    return;
                }
                if (rowsPerPage <= 0)
                {
                    txtRowsPerPage.Text = _rowsPerPage.ToString();
                    return;
                }
                _rowsPerPage = rowsPerPage;
                SetRowCount(_rowCount);
                if (PageJumpTo != null)
                {
                    PageJumpTo(_currentPage, _rowsPerPage);
                }
            }
        }
    }
}

4.把UGridePage控件与dataGrid放到同一个面板容器上(dataGrid的Anchor设置成Top, Bottom, Left, Right,UGridePage属性Dock设置成Bottom)

在这里插入图片描述
5.设置UGridPage的PageJumpTo事件
在这里插入图片描述

private void uGridPage1_PageJumpTo(int pageIndex, int rowsPerPage)
{
    var resultlist = EntityPublic.Instance.GetPageRowsFromSql<GT_DISPATCHINGMAIN>(sqlQuery, pageIndex, rowsPerPage, "DT_OPERATEDATE desc");
    if (resultlist != null && resultlist.Count > 0)
    {
        bsLot.DataSource = resultlist;
    }
    else
    {
        bsLot.DataSource = new List<GT_DISPATCHINGMAIN>();
    }
}
       /// <summary>
        /// Grid分页功能,得到Sql执行第几页的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">Sql</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="rowsPerPage">每页行数</param>
        /// <param name="orderByColum">排序列</param>
        /// <returns></returns>
        public List<T> GetPageRowsFromSql<T>(string sql, int pageIndex, int rowsPerPage, string orderByColum) where T : new()
        {
            Splasher.Show();
            string sqlQuery = string.Format(@"select * from (
select t.*, row_number() over(order by {3}) rownumber from (
{0}
) t
) where rownumber>=(({1}-1)*{2})+1 and rownumber<={1}*{2}", sql, pageIndex, rowsPerPage, orderByColum);
            var resultlist = this.GetEntityForSQL<T>(sqlQuery);
            Splasher.Close();
            return resultlist;
        }

7.数据查询
在这里插入图片描述

 sqlQuery = string.Format(@"Select a.nv_voucherid,a.nv_operatorname,decode(a.nv_operatetype,'0','发料','3','不良处理','T','退料','返工') as nv_operatetype,a.dt_operatedate,a.nv_fromvoucherid,a.vc_batno,
                  p.nv_processname,ap.nv_processname As nv_aprocessname,a.vc_invcode,a.NV_OPERATOR,
                  a.dc_tqty,a.dc_sqty,a.dc_fqty,a.dc_wqty,a.vc_minvcode,i.nv_invname,NVL(a.nv_fromvoucherid1,'') As NV_FROMVOUCHERID1,
                  '录入单' as operate1,'供应商信息' as operate2
From gt_dispatchingmain a
Join gt_inventory i on a.vc_invcode = i.vc_invcode and a.org_rrn=i.org_rrn
Left Join gt_process p on a.vc_processid = p.vc_processid and a.org_rrn=p.org_rrn
Left Join gt_process ap on a.vc_aprocessid = ap.vc_processid   and a.org_rrn=ap.org_rrn             
Where a.ch_flag = '05' and a.org_rrn={1} {0}", conditionStr,ConnectionInfo.OrgRrn);
            int rowscount = EntityPublic.Instance.GetRowsCountFromSql(sqlQuery);
            uGridPage1.SetRowCount(rowscount);
            if (rowscount>0)
            {
                var resultlist = EntityPublic.Instance.GetPageRowsFromSql<GT_DISPATCHINGMAIN>(sqlQuery, 1, uGridPage1.RowsPerPage, "DT_OPERATEDATE desc");
                bsLot.DataSource = resultlist;
            }
            else
            {
                ExceptionMsgBox.This.ShowMessageBox(" 未能查找到相关数据!");
                bsLot.Clear(); 
            }
  /// <summary>
        /// Grid分页功能,得到Sql执行第几页的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">Sql</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="rowsPerPage">每页行数</param>
        /// <param name="orderByColum">排序列</param>
        /// <returns></returns>
        public List<T> GetPageRowsFromSql<T>(string sql, int pageIndex, int rowsPerPage, string orderByColum) where T : new()
        {
            Splasher.Show();
            string sqlQuery = string.Format(@"select * from (
select t.*, row_number() over(order by {3}) rownumber from (
{0}
) t
) where rownumber>=(({1}-1)*{2})+1 and rownumber<={1}*{2}", sql, pageIndex, rowsPerPage, orderByColum);
            var resultlist = this.GetEntityForSQL<T>(sqlQuery);
            Splasher.Close();
            return resultlist;
        }
  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值