分页查询的自定义控件编写及使用

先上效果图 

 

具体操作如下:

1.自定义控件的编写

在项目中右键,添加---用户控件,然后选择Windows Forms,选择用户控件,设置控件名称(注意:名称不能使用UserControl,会和继承的父类重名),如下图所示;

 

2.根据需要,在自定义控件上添加控件,如下

 

3.为自定义控件Page添加按钮事件,代码如下 

public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }


        private int record = 0;//总数据信息

        /// <summary>
        /// 总记录数
        /// </summary>
        public int Record
        {
            get { return record; }
            set
            {
                record = value;
                InitPageInfo();
            }
        }

        private int pageSize = 100;//每一页的数据量

        /// <summary>
        /// 每页条数
        /// </summary>
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        private int currentPage = 1;//当前页码

        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPage
        {
            get { return currentPage; }
            set { currentPage = value; }
        }

        
        public int pageNum = 0;//总页码

        /// <summary>
        /// 总页码
        /// </summary>
        public int PageNum
        {
            get
            {
                if (Record == 0)
                {
                    pageNum = 0;
                }
                else
                {
                    if (Record % PageSize > 0)
                    {
                        pageNum = Record / PageSize + 1;
                    }
                    else
                    {
                        pageNum = Record / PageSize;
                    }
                }
                return pageNum;
            }

        }

        //定义委托
        public delegate void BindHandle(object sender, EventArgs e);

        /// <summary>
        /// 绑定数据源事件
        /// </summary>
        public event BindHandle BindSource;//在调用本控件的界面中添加此事件,即可进行事件的操作

        /// <summary>
        /// 初始化页面
        /// </summary>
        private void InitPageInfo()
        {
            if (Record == 0 || (Record > 0 && CurrentPage > pageNum))
            {
                CurrentPage = 1;
            }
            lblInfo.Text = string.Format("共 {0} 条记录  共 {1} 页  当前第 {2} 页", Record, PageNum, CurrentPage);
            txtPage.Text = CurrentPage.ToString();

        }

        /// <summary>
        /// 页面初始化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Page_Load(object sender, EventArgs e)
        {
            if (BindSource != null)
            {
                BindSource(sender, e);
                InitPageInfo();
            }

            BtnEnabled();
        }

        /// <summary>
        /// 按钮可用性判断
        /// </summary>
        private void BtnEnabled()
        {
            //按钮可用性设置
            if (currentPage <= 1)
            {
                btnFirst.Enabled = false;
                btnPre.Enabled = false;
            }
            else
            {
                btnFirst.Enabled = true;
                btnPre.Enabled = true;
            }
            if (currentPage == pageNum)
            {
                btnNext.Enabled = false;
                btnLast.Enabled = false;
            }
            else
            {
                btnNext.Enabled = true;
                btnLast.Enabled = true;
            }
        }


        /// <summary>
        /// 首页按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFirst_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (CurrentPage == 1)
                {
                    XtraMessageBox.Show("当前已经是首页","提示");
                    return;
                }
                else
                {
                    CurrentPage = 1;
                    if (BindSource != null)
                    {
                        BindSource(sender, e);
                        InitPageInfo();
                    }
                }
            }

            BtnEnabled();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPre_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (CurrentPage == 1)
                {
                    XtraMessageBox.Show("当前已经是首页","提示");
                    return;
                }
                else
                {
                    CurrentPage = CurrentPage - 1;
                    if (BindSource != null)
                    {
                        BindSource(sender, e);
                        InitPageInfo();
                    }
                }
            }
            BtnEnabled();
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (CurrentPage == PageNum)
                {
                    XtraMessageBox.Show("当前已经是末页","提示");
                    return;
                }
                else
                {
                    CurrentPage = CurrentPage + 1;
                    if (BindSource != null)
                    {
                        BindSource(sender, e);
                        InitPageInfo();
                    }
                }
            }
            BtnEnabled();
        }

        /// <summary>
        /// 末页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLast_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (CurrentPage == PageNum)
                {
                    XtraMessageBox.Show("当前已经是末页","提示");
                    return;
                }
                else
                {
                    CurrentPage = PageNum;
                    if (BindSource != null)
                    {
                        BindSource(sender, e);
                        InitPageInfo();
                    }
                }
            }
            BtnEnabled();
        }

        /// <summary>
        /// 跳转事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGo_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (txtPage.Text.Trim() != null && txtPage.Text.Trim() != "")
                {
                    if (!Regex.IsMatch(txtPage.Text.Trim(), @"^[\d]*$"))
                    {
                        XtraMessageBox.Show("请正确填写页码!", "提示");
                        return;
                    }
                }
                else
                {
                    XtraMessageBox.Show("请填写页码!", "提示");
                    return;
                }

                int page = Convert.ToInt32(txtPage.Text.Trim());
                if (page == 0)
                {
                    page = 1;
                }
                if (page > PageNum)
                {
                    page = PageNum;
                }

                CurrentPage = page;
                if (BindSource != null)
                {
                    BindSource(sender, e);
                    InitPageInfo();
                }
            }
            BtnEnabled();
        }
    }

4.将控件添加至所需的页面

先运行程序,运行成功后关闭程序,在工具栏中即可看到之前添加的自定义控件,将控件添加至所需界面

5.在添加了Page控件的页面中点击Page控件,打开属性,选择事件,选择BindSource事件,添加该事件

6.在page1_BindSource事件中添加数据源

private void page1_BindSource(object sender, EventArgs e)
        {
            int num;//记录总数目
            //分页查询
            list = stepInfoClientBusiness.GetStepInfoPageInfo_MySet(page1.PageSize, page1.CurrentPage, GlobalVariables.LoginEmployee.EmployeeId, out num);
            //赋值
            page1.Record = num;
            //数据源赋值
            gridControl1.DataSource = list;
            gridView1.RefreshData();

            if (list != null)
            {
                gridView1.GroupPanelText = "有" + list.Count + "条记录";
            }
            else
            {
                gridView1.GroupPanelText = "有 0 条记录";
            }
        }

7.运行结果如图:

 

 总结:本博客参考了其他博文(Winform自定义分页控件的实现),在此基础上做了一定的优化,添加了按钮可用性判断,并且详细说明了如何调用自定义控件中申明的事件,之前就是卡在事件的调用上,后经他人指导,完成调用.在此记录,以便需要之人参考.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值