http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 44392|回复: 42
打印 上一主题 下一主题

[小工具] C#Winfrom分页控件--WinFormPage

[复制链接]
跳转到指定楼层
楼主
发表于 2012-12-17 11:30:21 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
这个控件我用一段时间了,感觉还是可以用的,分享出来给大家吧。
先来看看我的效果吧

源码下载:
WinFormPage.rar (30.42 KB, 下载次数: 2547)
代码实现如下
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WinFormPage
{
    /// <summary>
    /// 更新网站:[url]http://www.sufeinet.com/thread-1788-1-1.html[/url]
    /// </summary>
    public partial class WinFormPage : UserControl
    {
        public delegate void RefreshPage();
        private RefreshPage _refresh;
        /// <summary>
        /// 页显示数
        /// </summary>
        private int _PageSize = 20;
        /// <summary>
        /// 页总数
        /// </summary>
        private int _PageCount = 0;
        /// <summary>
        ///  页码
        /// </summary>
        private int _PageIndex = 1;
        /// <summary>
        /// 数据条数
        /// </summary>
        private int _Count = 0;
        /// <summary>
        /// 跳转页码
        /// </summary>
        private int _GoIndex = 0;

        public void isEnable()
        {
            try
            {
                if (_PageIndex == 1)
                {
                    this.lbtnfrist.Enabled = false;
                    this.lbtnUP.Enabled = false;
                }
                else
                {
                    this.lbtnUP.Enabled = true;
                    this.lbtnfrist.Enabled = true;
                }

                if (this._PageIndex == this._PageCount)
                {
                    this.lbtnDown.Enabled = false;
                    this.lbtnlast.Enabled = false;
                }
                else
                {
                    this.lbtnDown.Enabled = true;
                    this.lbtnlast.Enabled = true;

                }
                if (this._Count == 0)
                {
                    this.lbtnDown.Enabled = false;
                    this.lbtnlast.Enabled = false;
                    this.lbtnfrist.Enabled = false;
                    this.lbtnUP.Enabled = false;
                }
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// 获取或设置页显示数量
        /// </summary>
        public int PageSize
        {
            get { return _PageSize; }
            set { _PageSize = value; }
        }

        /// <summary>
        /// 获取或设置页数量
        /// </summary>
        public int PageCount
        {
            get { return _PageCount; }
            set
            {
                _PageCount = value;
                labpcount.Text = _PageCount.ToString();
            }
        }

        /// <summary>
        /// 获取或设置页码
        /// </summary>
        public int PageIndex
        {
            get { return Convert.ToInt32(labindex.Text); }
            set { _PageIndex = value; }
        }

        /// <summary>
        /// 获取或设置数据总数量
        /// </summary>
        public int Count
        {
            get
            {
                return _Count;
            }
            set
            {
                _Count = value;
            }
        }
        /// <summary>
        /// 获取或设置跳转页面
        /// </summary>
        public int GoIndex
        {
            get { return _GoIndex; }
            set { _GoIndex = value; }
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        public RefreshPage RefreshData
        {
            set
            {
                _refresh = value;

            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public WinFormPage()
        {
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //第一页
            _PageIndex = 1;
            labindex.Text = _PageIndex.ToString();
            _refresh();
            isEnable();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //上一页
            int tmp = Convert.ToInt32(labindex.Text);
            tmp = tmp - 1;
            if (tmp <= 0)
            {
                tmp = 1;
            }
            _PageIndex = tmp;
            labindex.Text = _PageIndex.ToString();
            _refresh();
            isEnable();
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //下一页
            int tmp = Convert.ToInt32(labpcount.Text);
            int tmp2 = Convert.ToInt32(labindex.Text);
            tmp2 = tmp2 + 1;
            if (tmp2 > tmp)
            {
                _PageIndex = tmp;
                labindex.Text = tmp.ToString();
            }
            else
            {
                _PageIndex = tmp2;
                labindex.Text = tmp2.ToString();
            }
            _refresh();
            isEnable();
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //最后页
            _PageIndex = Convert.ToInt32(labpcount.Text);
            labindex.Text = labpcount.Text;
            _refresh();
            isEnable();
        }

        private void linkLabel5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //跳转
            int tmp = 0;
            try
            {
                tmp = Convert.ToInt32(tbxGo.Text);
            }
            catch
            {
                tmp = 1;
                tbxGo.Text = "1";
            }
            if (tmp <= 0)
            {
                tmp = 1;
            }
            int tmp2 = Convert.ToInt32(labpcount.Text);
            if (tmp > tmp2)
            {
                _PageIndex = tmp2;
            }
            else
            {
                _PageIndex = tmp;
            }
            labindex.Text = _PageIndex.ToString();
            tbxGo.Text = _PageIndex.ToString();
            _refresh();
            isEnable();
        }
    }
}

使用方法
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //分页方法
        private void bind()
        {
            //取总行数
            winFormPage1.Count = 1000;
            //总页数
            winFormPage1.PageCount = winFormPage1.Count / winFormPage1.PageSize;

            if (winFormPage1.Count % winFormPage1.PageSize != 0)
                winFormPage1.PageCount = winFormPage1.PageCount + 1;
            //绑定数
            dataGridView1.DataSource = "数据源";

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //刷新分页方法
            winFormPage1.RefreshData = bind;
            bind();
        }
    }
}

大家可以测试一下看看怎么样

本帖被以下淘专辑推荐:



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
沙发
发表于 2012-12-20 15:05:58 | 只看该作者
我的Winform就没有分过页,试一下
板凳
 楼主| 发表于 2012-12-20 15:19:10 | 只看该作者
weiyz2011 发表于 2012-12-20 15:05
我的Winform就没有分过页,试一下

如果在使用中有问题,可以直接提出来
地板
发表于 2012-12-21 11:53:43 | 只看该作者
  //总页数
            winFormPage1.PageCount = winFormPage1.Count / winFormPage1.PageSize;

            if (winFormPage1.Count % winFormPage1.PageSize != 0)
                winFormPage1.PageCount = winFormPage1.PageCount + 1;
这些代码如果封装到分页控件里面会更好
5
 楼主| 发表于 2012-12-21 11:55:16 | 只看该作者
net_love 发表于 2012-12-21 11:53
//总页数
            winFormPage1.PageCount = winFormPage1.Count / winFormPage1.PageSize;

好的,谢谢建议会的
6
发表于 2012-12-21 11:57:10 | 只看该作者
嗯,如果多次使用,这些代码都是重复的
7
发表于 2012-12-29 22:54:02 | 只看该作者
感谢楼主分享.
8
发表于 2013-1-5 14:26:44 | 只看该作者
谢谢老大分享!下载了!
9
发表于 2013-1-7 23:56:26 | 只看该作者
谢谢老大分享!下载了!
10
发表于 2013-2-28 09:12:29 | 只看该作者
有没写个demo看个效果貌似不怎么会用
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-3-19 11:28

© 2014-2021

快速回复 返回顶部 返回列表