【C#】DataGridView的分页功能

1、分页整体显示如下:

主要包括功能如下:

1)显示总页数、当前页数

2)向前,向后翻页功能

3)显示首页,显示最后一页功能

4)自定义跳转页面功能

 

2、页面设计部分(.Designer.cs文件):

partial class DGVPager
    {
        /// <summary> 
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary> 
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.label5 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.linkLast = new System.Windows.Forms.Button();
            this.linkFirst = new System.Windows.Forms.Button();
            this.linkPrevious = new System.Windows.Forms.Button();
            this.linkNext = new System.Windows.Forms.Button();
            this.lblPageCount = new System.Windows.Forms.Label();
            this.lblSept = new System.Windows.Forms.Label();
            this.lblTotalCount = new System.Windows.Forms.Label();
            this.lblCurrentPage = new System.Windows.Forms.Label();
            this.btnGo = new System.Windows.Forms.Button();
            this.txtPageNum = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Font = new System.Drawing.Font("幼圆", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label5.Location = new System.Drawing.Point(196, 6);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(49, 14);
            this.label5.TabIndex = 264;
            this.label5.Text = "条记录";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("幼圆", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label3.Location = new System.Drawing.Point(1, 6);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(63, 14);
            this.label3.TabIndex = 263;
            this.label3.Text = "当前页:";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("幼圆", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label1.Location = new System.Drawing.Point(145, 6);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(21, 14);
            this.label1.TabIndex = 261;
            this.label1.Text = "";
            // 
            // linkLast
            // 
            this.linkLast.Font = new System.Drawing.Font("幼圆", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.linkLast.Location = new System.Drawing.Point(410, 1);
            this.linkLast.Name = "linkLast";
            this.linkLast.Size = new System.Drawing.Size(44, 23);
            this.linkLast.TabIndex = 260;
            this.linkLast.Text = ">>|";
            this.linkLast.UseVisualStyleBackColor = true;
            this.linkLast.Click += new System.EventHandler(this.linkLast_Click);
            // 
            // linkFirst
            // 
            this.linkFirst.Font = new System.Drawing.Font("幼圆", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.linkFirst.Location = new System.Drawing.Point(255, 1);
            this.linkFirst.Name = "linkFirst";
            this.linkFirst.Size = new System.Drawing.Size(44, 23);
            this.linkFirst.TabIndex = 259;
            this.linkFirst.Text = "|<<";
            this.linkFirst.UseVisualStyleBackColor = true;
            this.linkFirst.Click += new System.EventHandler(this.linkFirst_Click);
            // 
            // linkPrevious
            // 
            this.linkPrevious.Font = new System.Drawing.Font("幼圆", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 中,实现 DataGridView 分页功能可以通过以下步骤实现: 1. 创建一个 DataGridView 控件,用于显示数据。 2. 获取要显示的数据,并将其存储在一个数据源中(如 DataTable 或 List)。 3. 设置 DataGridView 的数据源为该数据源。 4. 设置 DataGridView分页参数,包括每页显示的行数和当前页码。 5. 根据当前页码和每页显示的行数,从数据源中获取对应的数据,并更新到 DataGridView 中。 6. 通过按钮或其他控件来改变当前页码,并重新加载对应的数据。 以下是一个简单的示例代码,演示了如何实现分页功能: ```csharp // 初始化 DataGridView DataGridView dataGridView = new DataGridView(); dataGridView.Dock = DockStyle.Fill; // 获取要显示的数据 List<string> data = GetData(); // 设置每页显示的行数和当前页码 int pageSize = 10; int currentPage = 1; // 更新 DataGridView 数据 UpdateDataGridView(dataGridView, data, pageSize, currentPage); // 按钮点击事件,切换到下一页 void NextPageButton_Click(object sender, EventArgs e) { currentPage++; UpdateDataGridView(dataGridView, data, pageSize, currentPage); } // 更新 DataGridView 数据的方法 void UpdateDataGridView(DataGridView dataGridView, List<string> data, int pageSize, int currentPage) { // 计算起始索引和结束索引 int startIndex = (currentPage - 1) * pageSize; int endIndex = Math.Min(startIndex + pageSize - 1, data.Count - 1); // 创建一个新的数据源,包含当前页的数据 List<string> pageData = data.Skip(startIndex).Take(pageSize).ToList(); // 将数据源绑定到 DataGridView dataGridView.DataSource = pageData; // 更新 DataGridView 的显示 dataGridView.Refresh(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值