C#开发WinForm分页控件_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C#开发WinForm分页控件

C#开发WinForm分页控件

 2013/8/25 12:55:18  蒋叶湖  博客园  我要评论(0)
  • 摘要:WinFormPager.dll控件下载地址WinFormPager源代码下载地址以下是调用分页控件WinFormPager方法://第一步:指定返回的记录数winFormPager1.RecordCount=返回记录数;//第二步:在控件的PageChanged事件中执行绑定DataGridView的方法privatevoidwinFormPager1_PageChanged(){dataGridView1.DataSource=GetList(winFormPager1.PageSize
  • 标签:C# for 开发 控件 winform
   WinFormPager.dll控件下载地址 WinFormPager源代码下载地址

以下是调用分页控件WinFormPager方法

//第一步:指定返回的记录数

winFormPager1.RecordCount = 返回记录数;

//第二步:在控件的PageChanged事件中执行绑定DataGridView的方法

private void winFormPager1_PageChanged()
 {
     dataGridView1.DataSource = GetList(winFormPager1.PageSize,winFormPager1.CurrentPage);//GetList为获取数据库记录方法,不介绍
 }

//注:完成以上步骤就可成功调用,其余PageSize属性等可在属性浏览器中设置

以下是分页控件WinformPager完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace WinFormPager
{
    public partial class WinFormPager : UserControl
   
{
        int currentPage = 1;//当前页
        /// <summary>
        /// 当前页
        /// </summary>
        [Description("当前页"), Category("分页设置")]
        public int CurrentPage
        {
            get { return currentPage; }
            set { currentPage = value; }
        }
        int pageSize = 10;//每页显示条数
        /// <summary>
        /// 每页显示条数
        /// </summary>
        [Description("每页显示条数"), Category("分页设置")]
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }
        int pageTotal = 0;//总共多少页
        /// <summary>
        /// 总共多少页
        /// </summary>
        [Description("总共多少页"), Category("分页设置")]
        public int PageTotal
        {
            get { return pageTotal; }
            set { pageTotal = value; }
        }
        int currentGroup = 1;//当前组
        /// <summary>
        /// 当前组
        /// </summary>
        [Description("当前组"), Category("分页设置")]
        public int CurrentGroup
        {
            get { return currentGroup; }
            set { currentGroup = value; }
        }
        int groupSize = 10;//每组显示页数
        /// <summary>
        /// 每组显示页数
        /// </summary>
        [Description("每组显示页数"), Category("分页设置")]
        public int GroupSize
        {
            get { return groupSize; }
            set { groupSize = value; }
        }
        int groupTotal = 0;//总共多少组
        /// <summary>
        /// 总共多少组
        /// </summary>
        [Description("总共多少组"), Category("分页设置")] 
        public int GroupTotal
        {
            get { return groupTotal; }
            set { groupTotal = value; }
        }
        /// <summary>
        /// 总的记录数
        /// </summary>
        private int recordCount;//总的记录数
        [Description("总的记录数"), Category("分页设置")]
        public int RecordCount
        {
            get { return recordCount; }
            set
            {
                recordCount = value;
                InitData();// 初始化数据
                PageChanged();//当前页改变事件
            }
        }
        private int buttonWidth = 20;//按钮宽度
        /// <summary>
        /// 按钮宽度
        /// </summary>
        [Description("按钮宽度"), Category("分页设置")]
        public int ButtonWidth
        {
            get { return buttonWidth; }
            set { buttonWidth = value; }
        }
        private int buttonHeight = 23;//按钮高度
        /// <summary>
        /// 按钮高度
        /// </summary>
        [Description("按钮高度"), Category("分页设置")]
        public int ButtonHeight
        {
            get { return buttonHeight; }
            set { buttonHeight = value; }
        }
        private int buttonDistance = 0;//按钮间距离
        /// <summary>
        /// 按钮间距离
        /// </summary>
        [Description("按钮间距离"), Category("分页设置")]
        public int ButtonDistance
        {
            get { return buttonDistance; }
            set { buttonDistance = value; }
        }
        List<Control> listControl = new List<Control>();//分页的按钮集合

        public delegate void PageChangeDelegate();
        /// <summary>
        /// 当前页改变时发生的事件
        /// </summary>
        [Description("当前页改变时发生的事件"), Category("分页设置")]
        public event PageChangeDelegate PageChanged;
        public WinFormPager()
        {
            InitializeComponent();
            PageChanged = SetBtnPrePageAndBtnNextPage;
            PageChanged();
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            PageTotal = RecordCount / PageSize;//总共多少页           
            if (RecordCount % PageSize != 0)
            {
                PageTotal++;
            }
            GroupTotal = PageTotal / GroupSize;//总共多少组
            if (PageTotal % GroupSize != 0)
            {
                GroupTotal++;
            }
            for (int i = 0; i < PageTotal; i++)
            {
                cmbPage.Items.Add((i + 1).ToString());//添加下拉框值
            }
            BuildPageControl();//创建分页数字按钮      
        }
        /// <summary>
        /// 创建分页数字按钮
        /// </summary>
        private void BuildPageControl()
        {
            int x = 0;//按钮横坐标
            int y = 0;//按钮纵坐标
            int num = 0;//按钮数
            for (int i = GroupSize * (CurrentGroup - 1); i < GroupSize * CurrentGroup; i++)
            {
                if (i + 1 > PageTotal)
                {
                    break;
                }
                num++;
            }
            int xBtnPreGroup = x + (ButtonWidth + ButtonDistance) * num;//btnPerGroup横坐标
            //指定上一组 下一组 上一页 下一页 坐标
            btnPreGroup.Location = new Point(xBtnPreGroup, y);
            btnNextGroup.Location = new Point(btnPreGroup.Location.X + btnPreGroup.Width + ButtonDistance, y);
            btnPrePage.Location = new Point(btnNextGroup.Location.X + btnNextGroup.Width + ButtonDistance, y);
            btnNextPage.Location = new Point(btnPrePage.Location.X + btnPrePage.Width + ButtonDistance, y);
            cmbPage.Location = new Point(btnNextPage.Location.X + btnNextPage.Width + ButtonDistance, y+(ButtonHeight-cmbPage.Height)/2);
            btnGo.Location = new Point(cmbPage.Location.X + cmbPage.Width + ButtonDistance, y);
            //设置整个控件的宽度、高度
            this.Width = btnGo.Location.X + btnGo.Width;
            this.Height = ButtonHeight;
            btnPreGroup.Height = ButtonHeight;
            btnNextGroup.Height = ButtonHeight;
            btnPrePage.Height = ButtonHeight;
            btnNextPage.Height = ButtonHeight;
            cmbPage.Height = ButtonHeight;
            btnGo.Height = ButtonHeight;
            //循环遍历移除控件
            foreach (Control c in listControl)
            {
                this.Controls.Remove(c);
            }
            listControl = new List<Control>();
            ButtonX button = null;
            //循环创建控件
            for (int i = GroupSize * (currentGroup - 1); i < GroupSize * CurrentGroup; i++)
            {
                if (i + 1 > PageTotal)
                {
                    break;
                }
                button = new ButtonX();
                button.Text = (i + 1).ToString();
                button.Width = ButtonWidth;
                button.Height = ButtonHeight;
                button.ColorTable = DevComponents.DotNetBar.eButtonColor.OrangeWithBackground;
                button.Location = new Point(x, y);
                button.Click += new EventHandler(button_Click);
                this.Controls.Add(button);
                button.BringToFront();
                listControl.Add(button);//添加进分页按钮的集合
                x += ButtonWidth + ButtonDistance;
            }
            //上一组是否可用
            if (CurrentGroup == 1)
            {
                btnPreGroup.Enabled = false;
            }
            else
            {
                btnPreGroup.Enabled = true;
            }
            //下一组是否可用
            if (CurrentGroup == GroupTotal)
            {
                btnNextGroup.Enabled = false;
            }
            else
            {
                btnNextGroup.Enabled = true;
            }
        }
        /// <summary>
        /// 数字按钮分页
        /// </summary>
        private void button_Click(object sender, EventArgs e)
        {
            CurrentPage = int.Parse((sender as ButtonX).Text);
            PageChanged();
        }
        /// <summary>
        /// 设置上一页、下一页是否可用以及当前页按钮字体颜色
        /// </summary>
        private void SetBtnPrePageAndBtnNextPage()
        {
            //上一页是否可用
            if (CurrentPage == 1)
            {
                btnPrePage.Enabled = false;
            }
            else
            {
                btnPrePage.Enabled = true;
            }
            //下一页是否可用
            if (CurrentPage == PageTotal)
            {
                btnNextPage.Enabled = false;
            }
            else
            {
                btnNextPage.Enabled = true;
            }
            //设置数字分页按钮文本颜色
            foreach (Control c in this.Controls)
            {
                //当前页字体为红色
                if (c.Text == CurrentPage.ToString() && c is ButtonX)
                {
                    c.ForeColor = Color.Red;
                }
                else
                {
                    c.ForeColor = Color.Blue;
                }
            }
        }
        /// <summary>
        /// 上一组
        /// </summary>
        private void btnPreGroup_Click(object sender, EventArgs e)
        {
            CurrentGroup--;
            BuildPageControl();
            CurrentPage = GroupSize * (CurrentGroup - 1) + 1; ;
            PageChanged();
        }
        /// <summary>
        /// 下一组
        /// </summary>
        private void btnNextGroup_Click(object sender, EventArgs e)
        {
            CurrentGroup++;
            BuildPageControl();
            CurrentPage = GroupSize * (CurrentGroup - 1) + 1; ;
            PageChanged();
        }
        /// <summary>
        /// 上一页
        /// </summary>
        private void btnPrePage_Click(object sender, EventArgs e)
        {
            //如果是当前组的第一页,直接上一组
            if (CurrentPage == GroupSize * (CurrentGroup - 1) + 1)
            {
                CurrentGroup--;
                BuildPageControl();
                CurrentPage --; ;
                PageChanged();
                return;
            }
            CurrentPage--;
            PageChanged();
        }
        /// <summary>
        /// 下一页
        /// </summary>
        private void btnNextPage_Click(object sender, EventArgs e)
        {
            //如果是当前组的最后一页,直接下一组
            if (CurrentPage == GroupSize * (CurrentGroup - 1) + GroupSize)
            {
                btnNextGroup_Click(null, null);
                return;
            }
            CurrentPage++;
            PageChanged();
        }
        /// <summary>
        /// 转到第几页
        /// </summary>
        private void btnGo_Click(object sender, EventArgs e)
        {
            try
            {
                CurrentPage = int.Parse(cmbPage.Text);
                PageChanged();
            }
            catch
            {
                MessageBox.Show("请输入数字");
            }
        }
    }
}

http://www.cnblogs.com/CareySon/archive/2009/10/04/1577948.html

http://www.cnblogs.com/CareySon/archive/2009/10/04/1577948.html

http://www.cnblogs.com/cate/wpf/

发表评论
用户名: 匿名