DevExpress 用户控件 分页(中)

说明:

1)具体调用请关注 看DevExpress 用户控件 分页(下)

datanavi_ButtonClick 是DataNavigator的ButtonClikc事件

视图设计器:


分页用户控件后台代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace CYSoft.TS.UI.StudentInfo
{
    public partial class PageCtrl : DevExpress.XtraEditors.XtraUserControl
    {

        /// <summary>
        /// 自定义事件 当前页改变事件
        /// </summary>
        public event EventHandler CurrentPageChange;

        #region 定义分页 字段和属性
        private int m_recordCount = 0;
        /// <summary>
        /// 数据行总数
        /// </summary>
        public int recordCount
        {
            get { return m_recordCount; }
            set
            {
                SetFormCtrlEnable();
                this.m_recordCount = value;
            }
        }

        private int m_pageSize = 20;
        /// <summary>
        /// 每页显示的数据行数
        /// </summary>
        public int pageSize
        {
            get { return m_pageSize; }
            set { this.m_pageSize = value; }
        }

        private int m_pageIndex = 1;
        /// <summary>
        /// 当前页
        /// </summary>
        public int pageIndex
        {
            get { return m_pageIndex; }
            set { this.m_pageIndex = value; }
        }

        private int m_pageCount = 0;
        /// <summary>
        /// 总页数
        /// </summary>
        public int pageCount
        {
            get
            {
                if (m_pageSize != 0)
                {
                    m_pageCount = GetPageCount();
                }
                return m_pageCount;
            }

        }

        /// <summary>
        /// 获取总页数
        /// </summary>
        /// <returns></returns>
        private int GetPageCount()
        {
            if (pageSize == 0)
            {
                return 0;
            }
            int num = 1;
            if (recordCount % pageSize == 0)
            {
                num = recordCount / pageSize;
            }
            else
            {
                num = recordCount / pageSize + 1;
            }
            return num;
        }
        #endregion

        public PageCtrl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 设置DataNavigator按钮全部可用
        /// </summary>
        private void SetFormCtrlEnable()
        {
            this.datanavi.Buttons.CustomButtons[0].Enabled = true;
            this.datanavi.Buttons.CustomButtons[1].Enabled = true;
            this.datanavi.Buttons.CustomButtons[2].Enabled = true;
            this.datanavi.Buttons.CustomButtons[3].Enabled = true;
        }

        //DataNavigator 点击      
        private void datanavi_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            ShowEvent("ButtonClick", e.Button);
        }

        #region 第一页、上一页、下一页、最后一页
        /// <summary>
        /// 第一页、上一页、下一页、最后一页 点击事件
        /// </summary>
        /// <param name="eventString"></param>
        /// <param name="nbb"></param>
        void ShowEvent(string eventString, NavigatorButtonBase nbb)
        {
            switch (nbb.Tag.ToString())
            {
                case "First":
                    First();
                    break;
                case "PrePage":
                    PrePage();
                    break;
                case "NextPage":
                    NextPage();
                    break;
                case "Last":
                    Last();
                    break;
                default:
                    break;
            }
            DrawControl(true);
        }

        private void First()
        {
            m_pageIndex = 1;
        }
        private void PrePage()
        {
            m_pageIndex = Math.Max(1, m_pageIndex - 1);
        }
        private void NextPage()
        {
            m_pageIndex = Math.Min(pageCount, m_pageIndex + 1);
        }
        private void Last()
        {
            m_pageIndex = pageCount;
        }
        #endregion

        /// <summary>
        /// 设置DataNavigator按钮是否可用
        /// </summary>
        /// <param name="callEvent">是否当前页</param>
        private void DrawControl(bool callEvent)
        {
            if (callEvent && CurrentPageChange != null)
            {
                //当前页事件
                CurrentPageChange(this, null);
            }
            //设置按钮全部可用
            this.SetFormCtrlEnable();
            //只有一页
            if (recordCount <= 1)
            {
                this.datanavi.Buttons.CustomButtons[0].Enabled = false;
                this.datanavi.Buttons.CustomButtons[1].Enabled = false;
                this.datanavi.Buttons.CustomButtons[2].Enabled = false;
                this.datanavi.Buttons.CustomButtons[3].Enabled = false;
            }//第一页
            else if (pageIndex <= 1)
            {
                this.datanavi.Buttons.CustomButtons[0].Enabled = false;
                this.datanavi.Buttons.CustomButtons[1].Enabled = false;
            }//最后一页
            else if (pageIndex >= pageCount)
            {
                this.datanavi.Buttons.CustomButtons[2].Enabled = false;
                this.datanavi.Buttons.CustomButtons[3].Enabled = false;
            }

        }

        /// <summary>
        /// 加载LableControl
        /// </summary>
        private void ShowLableInfo()
        {
            if (!this.Created)
            {
                return;
            }
            this.BeginInvoke((MethodInvoker)delegate
            {
                try
                {
                    if (!DesignMode)
                    {
                        this.labelControl1.Text = "总行数:" + recordCount + "      当前页:" + pageIndex + "/" + pageCount + "    ";
                    }
                }
                catch { }
            });
        }

        /// <summary>
        /// 分页控件加载
        /// </summary>
        /// <param name="count">数据总行数</param>
        private void DrawControl(int count)
        {
            m_recordCount = count;
            //设置DataNavigator
            DrawControl(false);
            //设置LabControl
            ShowLableInfo();
        }

        /// <summary>
        /// 设置分页数据控件
        /// </summary>
        /// <param name="currentPageIndex">当前页</param>
        /// <param name="pageSize2">每页显示行数</param>
        /// <param name="recordTotal">总行数</param>
        public void SetPageCtrl(int currentPageIndex, int pageSize2, int recordTotal)
        {
            pageIndex = currentPageIndex;
            recordCount = recordTotal;
            pageSize = pageSize2;

            DrawControl(recordTotal);

        }

    }
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值