WinForm 分页控件

测试截图

WinForm 分页控件_第1张图片

控件设计

新建一个Pagination类,继承自System.Windows.Forms.UserControl。设定好用户控件的大小,托入一个TableLayoutPanel控件到UserControl上,根据实际情况布局好所需控件,如上图所示。(注:以下源码中的Button和TextBox继承自自定义的控件,非WinForm原生控件,因此部分属性原生控件可能不存在,请自行根据情况修改)

控件源码

namespace WinFormFramework.Controls
{
    [System.ComponentModel.DefaultEvent("PageChanged")]
    public class Pagination : System.Windows.Forms.UserControl
    {
        #region 构造
        public Pagination()
        {
            InitializeComponent();
            InitializeConfig();
        }
        #endregion

        #region 属性
        private Button ButtonFirstPage;
        private Button ButtonPreviousPage;
        private Button ButtonPage1;
        private Button ButtonPage2;
        private Button ButtonPage3;
        private Button ButtonPage4;
        private Button ButtonPage5;
        private Button ButtonNextPage;
        private Button ButtonLastPage;
        private Button ButtonGoToPage;
        private TextBox TextBoxPageNumber;
        private System.Windows.Forms.TableLayoutPanel TableLayoutPanelMain;

        private void InitializeComponent()
        {
            TableLayoutPanelMain = new System.Windows.Forms.TableLayoutPanel();
            ButtonFirstPage = new WinFormFramework.Controls.Button();
            ButtonPreviousPage = new WinFormFramework.Controls.Button();
            ButtonPage1 = new WinFormFramework.Controls.Button();
            ButtonPage2 = new WinFormFramework.Controls.Button();
            ButtonPage3 = new WinFormFramework.Controls.Button();
            ButtonPage4 = new WinFormFramework.Controls.Button();
            ButtonPage5 = new WinFormFramework.Controls.Button();
            ButtonNextPage = new WinFormFramework.Controls.Button();
            ButtonLastPage = new WinFormFramework.Controls.Button();
            ButtonGoToPage = new WinFormFramework.Controls.Button();
            TextBoxPageNumber = new WinFormFramework.Controls.TextBox();
            TableLayoutPanelMain.SuspendLayout();
            SuspendLayout();
            // 
            // TableLayoutPanelMain
            // 
            TableLayoutPanelMain.ColumnCount = 11;
            TableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 9F));
            TableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 9F));
            TableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 9F));
            TableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 9F));
            TableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 9F));
            TableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 9F));
            TableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 9F));
            TableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 9F));
            TableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 9F));
            TableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F));
            TableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 9F));
            TableLayoutPanelMain.Controls.Add(ButtonFirstPage, 0, 0);
            TableLayoutPanelMain.Controls.Add(ButtonPreviousPage, 1, 0);
            TableLayoutPanelMain.Controls.Add(ButtonPage1, 2, 0);
            TableLayoutPanelMain.Controls.Add(ButtonPage2, 3, 0);
            TableLayoutPanelMain.Controls.Add(ButtonPage3, 4, 0);
            TableLayoutPanelMain.Controls.Add(ButtonPage4, 5, 0);
            TableLayoutPanelMain.Controls.Add(ButtonPage5, 6, 0);
            TableLayoutPanelMain.Controls.Add(ButtonNextPage, 7, 0);
            TableLayoutPanelMain.Controls.Add(ButtonLastPage, 8, 0);
            TableLayoutPanelMain.Controls.Add(ButtonGoToPage, 10, 0);
            TableLayoutPanelMain.Controls.Add(TextBoxPageNumber, 9, 0);
            TableLayoutPanelMain.Dock = System.Windows.Forms.DockStyle.Fill;
            TableLayoutPanelMain.Location = new System.Drawing.Point(0, 0);
            TableLayoutPanelMain.Name = "TableLayoutPanelMain";
            TableLayoutPanelMain.RowCount = 1;
            TableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            TableLayoutPanelMain.Size = new System.Drawing.Size(330, 24);
            TableLayoutPanelMain.TabIndex = 0;
            // 
            // ButtonFirstPage
            // 
            ButtonFirstPage.BorderColor = System.Drawing.Color.LightGray;
            ButtonFirstPage.Dock = System.Windows.Forms.DockStyle.Fill;
            ButtonFirstPage.FillColor = System.Drawing.Color.White;
            ButtonFirstPage.HoverColor = System.Drawing.Color.LightGray;
            ButtonFirstPage.Location = new System.Drawing.Point(0, 0);
            ButtonFirstPage.Margin = new System.Windows.Forms.Padding(0);
            ButtonFirstPage.Name = "ButtonFirstPage";
            ButtonFirstPage.PressColor = System.Drawing.Color.DarkGray;
            ButtonFirstPage.Radius = 0;
            ButtonFirstPage.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            ButtonFirstPage.Size = new System.Drawing.Size(29, 24);
            ButtonFirstPage.TabIndex = 0;
            ButtonFirstPage.Text = "<<";
            ButtonFirstPage.Click += new System.EventHandler(ButtonFirstPage_Click);
            // 
            // ButtonPreviousPage
            // 
            ButtonPreviousPage.BorderColor = System.Drawing.Color.LightGray;
            ButtonPreviousPage.Dock = System.Windows.Forms.DockStyle.Fill;
            ButtonPreviousPage.FillColor = System.Drawing.Color.White;
            ButtonPreviousPage.HoverColor = System.Drawing.Color.LightGray;
            ButtonPreviousPage.Location = new System.Drawing.Point(29, 0);
            ButtonPreviousPage.Margin = new System.Windows.Forms.Padding(0);
            ButtonPreviousPage.Name = "ButtonPreviousPage";
            ButtonPreviousPage.PressColor = System.Drawing.Color.DarkGray;
            ButtonPreviousPage.Radius = 0;
            ButtonPreviousPage.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            ButtonPreviousPage.Size = new System.Drawing.Size(29, 24);
            ButtonPreviousPage.TabIndex = 1;
            ButtonPreviousPage.Text = "<";
            ButtonPreviousPage.Click += new System.EventHandler(ButtonPreviousPage_Click);
            // 
            // ButtonPage1
            // 
            ButtonPage1.BorderColor = System.Drawing.Color.LightGray;
            ButtonPage1.Dock = System.Windows.Forms.DockStyle.Fill;
            ButtonPage1.FillColor = System.Drawing.Color.White;
            ButtonPage1.HoverColor = System.Drawing.Color.LightGray;
            ButtonPage1.Location = new System.Drawing.Point(58, 0);
            ButtonPage1.Margin = new System.Windows.Forms.Padding(0);
            ButtonPage1.Name = "ButtonPage1";
            ButtonPage1.PressColor = System.Drawing.Color.DarkGray;
            ButtonPage1.Radius = 0;
            ButtonPage1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            ButtonPage1.Size = new System.Drawing.Size(29, 24);
            ButtonPage1.TabIndex = 2;
            ButtonPage1.Text = "1";
            ButtonPage1.Click += new System.EventHandler(ButtonPage_Click);
            // 
            // ButtonPage2
            // 
            ButtonPage2.BorderColor = System.Drawing.Color.LightGray;
            ButtonPage2.Dock = System.Windows.Forms.DockStyle.Fill;
            ButtonPage2.FillColor = System.Drawing.Color.White;
            ButtonPage2.HoverColor = System.Drawing.Color.LightGray;
            ButtonPage2.Location = new System.Drawing.Point(87, 0);
            ButtonPage2.Margin = new System.Windows.Forms.Padding(0);
            ButtonPage2.Name = "ButtonPage2";
            ButtonPage2.PressColor = System.Drawing.Color.DarkGray;
            ButtonPage2.Radius = 0;
            ButtonPage2.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            ButtonPage2.Size = new System.Drawing.Size(29, 24);
            ButtonPage2.TabIndex = 3;
            ButtonPage2.Text = "2";
            ButtonPage2.Click += new System.EventHandler(ButtonPage_Click);
            // 
            // ButtonPage3
            // 
            ButtonPage3.BorderColor = System.Drawing.Color.LightGray;
            ButtonPage3.Dock = System.Windows.Forms.DockStyle.Fill;
            ButtonPage3.FillColor = System.Drawing.Color.White;
            ButtonPage3.HoverColor = System.Drawing.Color.LightGray;
            ButtonPage3.Location = new System.Drawing.Point(116, 0);
            ButtonPage3.Margin = new System.Windows.Forms.Padding(0);
            ButtonPage3.Name = "ButtonPage3";
            ButtonPage3.PressColor = System.Drawing.Color.DarkGray;
            ButtonPage3.Radius = 0;
            ButtonPage3.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            ButtonPage3.Size = new System.Drawing.Size(29, 24);
            ButtonPage3.TabIndex = 4;
            ButtonPage3.Text = "3";
            ButtonPage3.Click += new System.EventHandler(ButtonPage_Click);
            // 
            // ButtonPage4
            // 
            ButtonPage4.BorderColor = System.Drawing.Color.LightGray;
            ButtonPage4.Dock = System.Windows.Forms.DockStyle.Fill;
            ButtonPage4.FillColor = System.Drawing.Color.White;
            ButtonPage4.HoverColor = System.Drawing.Color.LightGray;
            ButtonPage4.Location = new System.Drawing.Point(145, 0);
            ButtonPage4.Margin = new System.Windows.Forms.Padding(0);
            ButtonPage4.Name = "ButtonPage4";
            ButtonPage4.PressColor = System.Drawing.Color.DarkGray;
            ButtonPage4.Radius = 0;
            ButtonPage4.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            ButtonPage4.Size = new System.Drawing.Size(29, 24);
            ButtonPage4.TabIndex = 5;
            ButtonPage4.Text = "4";
            ButtonPage4.Click += new System.EventHandler(ButtonPage_Click);
            // 
            // ButtonPage5
            // 
            ButtonPage5.BorderColor = System.Drawing.Color.LightGray;
            ButtonPage5.Dock = System.Windows.Forms.DockStyle.Fill;
            ButtonPage5.FillColor = System.Drawing.Color.White;
            ButtonPage5.HoverColor = System.Drawing.Color.LightGray;
            ButtonPage5.Location = new System.Drawing.Point(174, 0);
            ButtonPage5.Margin = new System.Windows.Forms.Padding(0);
            ButtonPage5.Name = "ButtonPage5";
            ButtonPage5.PressColor = System.Drawing.Color.DarkGray;
            ButtonPage5.Radius = 0;
            ButtonPage5.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            ButtonPage5.Size = new System.Drawing.Size(29, 24);
            ButtonPage5.TabIndex = 6;
            ButtonPage5.Text = "5";
            ButtonPage5.Click += new System.EventHandler(ButtonPage_Click);

            // 
            // ButtonNextPage
            // 
            ButtonNextPage.BorderColor = System.Drawing.Color.LightGray;
            ButtonNextPage.Dock = System.Windows.Forms.DockStyle.Fill;
            ButtonNextPage.FillColor = System.Drawing.Color.White;
            ButtonNextPage.HoverColor = System.Drawing.Color.LightGray;
            ButtonNextPage.Location = new System.Drawing.Point(203, 0);
            ButtonNextPage.Margin = new System.Windows.Forms.Padding(0);
            ButtonNextPage.Name = "ButtonNextPage";
            ButtonNextPage.PressColor = System.Drawing.Color.DarkGray;
            ButtonNextPage.Radius = 0;
            ButtonNextPage.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            ButtonNextPage.Size = new System.Drawing.Size(29, 24);
            ButtonNextPage.TabIndex = 7;
            ButtonNextPage.Text = ">";
            ButtonNextPage.Click += new System.EventHandler(ButtonNextPage_Click);
            // 
            // ButtonLastPage
            // 
            ButtonLastPage.BorderColor = System.Drawing.Color.LightGray;
            ButtonLastPage.Dock = System.Windows.Forms.DockStyle.Fill;
            ButtonLastPage.FillColor = System.Drawing.Color.White;
            ButtonLastPage.HoverColor = System.Drawing.Color.LightGray;
            ButtonLastPage.Location = new System.Drawing.Point(232, 0);
            ButtonLastPage.Margin = new System.Windows.Forms.Padding(0);
            ButtonLastPage.Name = "ButtonLastPage";
            ButtonLastPage.PressColor = System.Drawing.Color.DarkGray;
            ButtonLastPage.Radius = 0;
            ButtonLastPage.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            ButtonLastPage.Size = new System.Drawing.Size(29, 24);
            ButtonLastPage.TabIndex = 8;
            ButtonLastPage.Text = ">>";
            ButtonLastPage.Click += new System.EventHandler(ButtonLastPage_Click);
            // 
            // ButtonGoToPage
            // 
            ButtonGoToPage.BorderColor = System.Drawing.Color.LightGray;
            ButtonGoToPage.Dock = System.Windows.Forms.DockStyle.Fill;
            ButtonGoToPage.FillColor = System.Drawing.Color.White;
            ButtonGoToPage.HoverColor = System.Drawing.Color.LightGray;
            ButtonGoToPage.Location = new System.Drawing.Point(294, 0);
            ButtonGoToPage.Margin = new System.Windows.Forms.Padding(0);
            ButtonGoToPage.Name = "ButtonGoToPage";
            ButtonGoToPage.PressColor = System.Drawing.Color.DarkGray;
            ButtonGoToPage.Radius = 0;
            ButtonGoToPage.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            ButtonGoToPage.Size = new System.Drawing.Size(36, 24);
            ButtonGoToPage.TabIndex = 9;
            ButtonGoToPage.Text = "Go";
            ButtonGoToPage.Click += new System.EventHandler(ButtonGoToPage_Click);
            // 
            // TextBoxPageNumber
            // 
            TextBoxPageNumber.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
            | System.Windows.Forms.AnchorStyles.Left
            | System.Windows.Forms.AnchorStyles.Right;
            TextBoxPageNumber.BackColor = System.Drawing.Color.White;
            TextBoxPageNumber.BorderColor = System.Drawing.Color.LightGray;
            TextBoxPageNumber.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            TextBoxPageNumber.HotBorderColor = System.Drawing.Color.DarkGray;
            TextBoxPageNumber.Location = new System.Drawing.Point(262, 1);
            TextBoxPageNumber.Margin = new System.Windows.Forms.Padding(1, 1, 0, 0);
            TextBoxPageNumber.Name = "TextBoxPageNumber";
            TextBoxPageNumber.Size = new System.Drawing.Size(32, 23);
            TextBoxPageNumber.TabIndex = 10;
            TextBoxPageNumber.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            TextBoxPageNumber.WatermarkText = "1";
            TextBoxPageNumber.Leave += new System.EventHandler(TextBoxPageNumber_Leave);
            TextBoxPageNumber.TextChanged += new System.EventHandler(TextBoxPageNumber_TextChanged);
            // 
            // Pagination
            // 
            BackColor = System.Drawing.Color.Transparent;
            Controls.Add(TableLayoutPanelMain);
            DoubleBuffered = true;
            Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
            Name = "Pagination";
            Size = new System.Drawing.Size(330, 24);
            TableLayoutPanelMain.ResumeLayout(false);
            TableLayoutPanelMain.PerformLayout();
            ResumeLayout(false);
        }
        #endregion

        #region 新增属性
        [System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("每页显示的数量。"), System.ComponentModel.DefaultValue(20)]
        public int PageSize { get; set; } = 20;

        private int totalCount = 0;
        [System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("总的序列数量。"), System.ComponentModel.DefaultValue(0)]
        public int TotalCount
        {
            get => totalCount;
            set
            {
                totalCount = value;
                GetTotalPage();
            }
        }
        #endregion

        #region 变量
        /// 
        /// 当前页码数
        /// 
        private int currentPage = 1;
        /// 
        /// 当前页码数
        /// 
        private int CurrentPage
        {
            get => currentPage;
            set
            {
                currentPage = value;
                UpdatePageButton();
            }
        }

        /// 
        /// 总页码数
        /// 
        private int totalPage = 0;
        /// 
        /// 总页码数
        /// 
        private int TotalPage
        {
            get => totalPage;
            set
            {
                totalPage = value;
                UpdatePageButton();
            }
        }

        /// 
        /// 当前页码的第一个序号
        /// 
        private int StartIndex { get; set; } = 0;
        #endregion

        #region 事件
        private void ButtonFirstPage_Click(object sender, System.EventArgs e)
        {
            CurrentPage = 1;
        }

        private void ButtonPreviousPage_Click(object sender, System.EventArgs e)
        {
            CurrentPage--;
        }

        private void ButtonPage_Click(object sender, System.EventArgs e)
        {
            Controls.Button button = (Controls.Button)sender;
            int page = System.Convert.ToInt32(button.Text);
            CurrentPage = page;
        }

        private void ButtonNextPage_Click(object sender, System.EventArgs e)
        {
            CurrentPage++;
        }

        private void ButtonLastPage_Click(object sender, System.EventArgs e)
        {
            CurrentPage = TotalPage;
        }

        private void ButtonGoToPage_Click(object sender, System.EventArgs e)
        {
            CurrentPage = System.Convert.ToInt32(TextBoxPageNumber.Text);
        }

        private void TextBoxPageNumber_Leave(object sender, System.EventArgs e)
        {
            if (!Helpers.RegularExpressions.IsNumber(TextBoxPageNumber.Text) || string.IsNullOrEmpty(TextBoxPageNumber.Text))
                TextBoxPageNumber.Text = CurrentPage.ToString();
        }

        private void TextBoxPageNumber_TextChanged(object sender, System.EventArgs e)
        {
            if (!string.IsNullOrEmpty(TextBoxPageNumber.Text))
            {
                if (!Helpers.RegularExpressions.IsNumber(TextBoxPageNumber.Text.Substring(TextBoxPageNumber.Text.Length - 1, 1)))
                    TextBoxPageNumber.Text = TextBoxPageNumber.Text.Substring(0, TextBoxPageNumber.Text.Length - 1);

                if (System.Convert.ToInt32(TextBoxPageNumber.Text) > TotalPage)
                    TextBoxPageNumber.Text = TotalPage.ToString();
            }

            TextBoxPageNumber.Select(TextBoxPageNumber.Text.Length, 0);
        }
        #endregion

        #region 新增事件
        public delegate void PageChangedHandler(int startIndex, int pageSize);
        public event PageChangedHandler PageChanged;
        #endregion

        #region 方法
        private void InitializeConfig()
        {
            UpdatePageButton();
        }

        /// 
        /// 启用按钮
        /// 
        /// 第一页
        /// 前一页
        /// 后一页
        /// 最后页
        private void EnableButton(bool firstPage, bool previousPage, bool nextPage, bool lastPage)
        {
            ButtonFirstPage.Enabled = firstPage;
            ButtonPreviousPage.Enabled = previousPage;
            ButtonNextPage.Enabled = nextPage;
            ButtonLastPage.Enabled = lastPage;
        }

        /// 
        /// 计算总的页码数
        /// 
        private void GetTotalPage()
        {
            TotalPage = System.Convert.ToInt32(System.Math.Ceiling((double)TotalCount / PageSize));
        }

        /// 
        /// 更新页码按钮
        /// 
        private void UpdatePageButton()
        {
            if (TotalPage > 1)
            {
                if (CurrentPage == 1)
                    EnableButton(false, false, true, true);
                else if (CurrentPage == TotalPage)
                    EnableButton(true, true, false, false);
                else if (CurrentPage > 1 && CurrentPage < totalPage)
                    EnableButton(true, true, true, true);
                else
                    EnableButton(false, false, false, false);

                System.Collections.Generic.List list = new System.Collections.Generic.List();
                if (TotalPage <= 5)
                    for (int i = 1; i <= TotalPage; i++)
                        list.Add(i);
                else
                {
                    if (CurrentPage <= 2)
                    {
                        list.Add(1);
                        list.Add(2);
                        list.Add(3);
                        list.Add(-1);
                        list.Add(TotalPage);
                    }
                    else if (CurrentPage > TotalPage - 2)
                    {
                        list.Add(1);
                        list.Add(-1);
                        list.Add(TotalPage - 2);
                        list.Add(TotalPage - 1);
                        list.Add(TotalPage);
                    }
                    else
                    {
                        list.Add(CurrentPage - 2);
                        list.Add(CurrentPage - 1);
                        list.Add(-1);
                        list.Add(CurrentPage + 1);
                        list.Add(CurrentPage + 2);
                    }
                }

                for (int i = 0; i < 5; i++)
                {
                    Controls.Button button = (Controls.Button)TableLayoutPanelMain.Controls.Find("ButtonPage" + (i + 1), false)[0];
                    if (i >= list.Count)
                        button.Enabled = false;
                    else
                    {
                        if (list[i] == -1)
                        {
                            button.Text = "...";
                            button.Enabled = false;
                        }
                        else
                        {
                            button.Text = list[i].ToString();
                            button.Enabled = true;
                        }
                    }

                    if (i < list.Count)
                    {
                        if (list[i] == CurrentPage)
                            button.ForeColor = System.Drawing.Color.Blue;
                        else
                            button.ForeColor = System.Drawing.SystemColors.ControlText;
                    }
                }

                ButtonGoToPage.Enabled = true;
            }
            else
            {
                EnableButton(false, false, false, false);
                ButtonPage1.Enabled = ButtonPage2.Enabled = ButtonPage3.Enabled = ButtonPage4.Enabled = ButtonPage5.Enabled = ButtonGoToPage.Enabled = false;
            }

            TextBoxPageNumber.Text = CurrentPage.ToString();
            StartIndex = (CurrentPage - 1) * PageSize;

            int count = 0;
            if (CurrentPage < TotalPage)
                count = PageSize;
            else if (CurrentPage == TotalPage)
                count = TotalCount - ((CurrentPage - 1) * PageSize);

            if (StartIndex < 0)
                StartIndex = 0;
            PageChanged?.Invoke(StartIndex, count);
        }
        #endregion
    }
}

调用

新建一个WinForm程序,在Form上拖入Pagination控件、ListView控件、Label控件。添加Form的Load事件以及Pagination的PageChanged事件。(如下图所示)

WinForm 分页控件_第2张图片

WinForm 分页控件_第3张图片 调用源码

        private void Form1_Load(object sender, System.EventArgs e)
        {
            PaginationTest.TotalCount = 945;
            ListViewTest.Columns.Add("Index");
            ListViewTest.Columns.Add("Number");
        }

        private void PaginationTest_PageChanged(int startIndex, int pageSize)
        {
            ListViewTest.Items.Clear();
            for (int i = startIndex; i < startIndex + pageSize; i++)
            {
                System.Windows.Forms.ListViewItem lvi = new System.Windows.Forms.ListViewItem((i + 1).ToString("000"));
                lvi.SubItems.Add((i + 1).ToString("000"));
                ListViewTest.Items.Add(lvi);
            }

            int page = ((startIndex + 1) / PaginationTest.PageSize) + 1;
            int totalPage = System.Convert.ToInt32(System.Math.Ceiling((double)PaginationTest.TotalCount / PaginationTest.PageSize));
            label1.Text = "总共 " + PaginationTest.TotalCount + " 条数据,每页显示 " + PaginationTest.PageSize + " 条,当前第 " + page + " 页, 共 " + totalPage + " 页";
        }

你可能感兴趣的:(windows,c#,.net)