我们如何在winform中的datagridview中进行分页 [英] How can we do pagination in datagridview in winform

查看:62
本文介绍了我们如何在winform中的datagridview中进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在窗口窗体的 datagridview 中每页显示 10 条记录,用户必须单击下一步按钮才能显示接下来的 10 条记录.DataGridview 中是否有某些属性,或者我是否需要创建自定义控件.

I want to show 10 records per page in a datagridview on a window form and user must click next button to show next 10 records. Is it there some property in DataGridview or do i need to create a custom control.

我需要做什么来实现这一目标.

What i need to do to achieve this.

推荐答案

这是一个简单的工作示例,其中BindingNavigator GUI 控件使用一个BindingSource反对通过将其数据源设置为 IListSource<的自定义子类来识别分页符/a>.(感谢这个答案关键思想.)当用户单击下一页"按钮时,BindingNavigator 会触发 bindingSource1_CurrentChanged 并且您的代码可以获取所需的记录.说明:

Here's a simple working example, where a BindingNavigator GUI control uses a BindingSource object to identify page breaks, by setting its DataSource to a custom subclass of IListSource. (Thanks to this answer for the key idea.) When the user clicks the "next page" button, the BindingNavigator fires bindingSource1_CurrentChanged and your code can fetch the desired records. Instructions:

  1. 创建 Windows 窗体应用程序
  2. 将 BindingNavigator、DataGridView 和 BindingSource 拖到表单上
  3. 将 Form1.cs 替换为以下代码:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace PagedDataGridView
{
    public partial class Form1 : Form
    {
        private const int totalRecords = 43;
        private const int pageSize = 10;

        public Form1()
        {
            InitializeComponent();
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { DataPropertyName = "Index" });
            bindingNavigator1.BindingSource = bindingSource1;
            bindingSource1.CurrentChanged += new System.EventHandler(bindingSource1_CurrentChanged);
            bindingSource1.DataSource = new PageOffsetList();
        }

        private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {
            // The desired page has changed, so fetch the page of records using the "Current" offset 
            int offset = (int)bindingSource1.Current;
            var records = new List<Record>();
            for (int i = offset; i < offset + pageSize && i < totalRecords; i++)
                records.Add(new Record { Index = i });
            dataGridView1.DataSource = records;
        }

        class Record
        {
            public int Index { get; set; }
        }

        class PageOffsetList : System.ComponentModel.IListSource
        {
            public bool ContainsListCollection { get; protected set; }

            public System.Collections.IList GetList()
            {
                // Return a list of page offsets based on "totalRecords" and "pageSize"
                var pageOffsets = new List<int>();
                for (int offset = 0; offset < totalRecords; offset += pageSize)
                    pageOffsets.Add(offset);
                return pageOffsets;
            }
        }
    }
}

这篇关于我们如何在winform中的datagridview中进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆