1. 程式人生 > >winform自定義控制元件——DataGridView

winform自定義控制元件——DataGridView

本例在winform中封裝了一個UserControl,主要使用了DataGridView,具備基本的分頁跳轉和顯示功能:

新建一個UserControl,做下佈局(本例取名:DataPager):

DataGridView用來顯示資料,兩個button用來上下翻頁,兩個lable分別顯示資料總數、當前頁碼數

程式碼如下:

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;

namespace Conf.UC
{
    public partial class DataPager : UserControl
    {
        public DataPager()
        {
            InitializeComponent();
        }

        DataTable dataTable;

        /// <summary>
        /// 顯示資料
        /// </summary>
        /// <param name="dt"></param>
        public void ShowData(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                // 倒序
                DataView dv = dt.DefaultView;
                dv.Sort = "id desc";
                DataTable dt2 = dv.ToTable();

                this.dataTable = dt2;
                this.page = 1;
            }
        }

        /// <summary>
        /// 總數
        /// </summary>
        int count
        {
            get
            {
                int value = dataTable.Rows.Count;
                this.lb_count.Text = value.ToString();
                return value;
            }
        }

        /// <summary>
        /// 總頁數
        /// </summary>
        int total_page
        {
            get
            {
                return (int)Math.Ceiling((double)count / (double)limit);
            }
        }

        int _page = 1;
        /// <summary>
        /// 當前頁碼
        /// </summary>
        int page
        {
            set
            {
                _page = value;
                // 範圍
                if (_page < 1)
                {
                    _page = 1;
                }
                if (_page > total_page)
                {
                    _page = total_page;
                }
                // 顯示資料
                DataTable dt = new DataTable();
                dt = dataTable.Clone();
                int start = (_page - 1) * limit;
                int end = start + limit;
                end = end > count ? count : end;
                for (int i = start; i < end; i++)
                {
                    dt.Rows.Add(dataTable.Rows[i].ItemArray);
                }
                this.data.DataSource = dt;
                // 顯示頁碼
                this.lb_page.Text = string.Format("{0}/{1}", _page, total_page);
                this.btnPre.Enabled = (_page == 1) ? false : true;
                this.btnNext.Enabled = (_page == total_page) ? false : true;
            }
            get { return _page; }
        }

        /// <summary>
        /// 每頁顯示數
        /// </summary>
        int limit = 50;

        // 上一頁
        private void btnPre_Click(object sender, EventArgs e)
        {
            page--;
        }

        // 下一頁
        private void btnNext_Click(object sender, EventArgs e)
        {
            page++;
        }
    }
}
若在vs工具箱列表中,沒有出現剛剛自定義的UserControl,需要點選vs->專案->重新整理專案工具箱項,即可重新整理工具箱列表。