我们怎样才能做到分页中的winform的datagridview [英] How can we do pagination in datagridview in winform
问题描述
我要显示在窗口的形式和用户在一个DataGridView,每页10笔必须点击下一步按钮,显示下10条记录。它是在有一定的DataGridView属性或做我需要创建一个自定义的控制。
我需要做什么来实现这一目标是什么。
下面是一个简单的工作例如,在
BindingNavigator GUI控件使用
<一href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource%28v=VS.80%29.aspx\">BindingSource反对
识别分页符,通过其DataSource设置的自定义子类<一href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.ilistsource.aspx\">IListSource.
(感谢<一个href=\"http://stackoverflow.com/questions/4629160/using-bindingnavigator-without-bindingsource/7420811#7420811\">this回答为
关键的想法)。当用户点击下一页按钮,BindingNavigator火灾 bindingSource1_CurrentChanged
和你的code可以获取所需的记录。说明:
- 创建一个Windows窗体应用程序
- 将到形式的BindingNavigator,一个DataGridView和BindingSource的
- 用下面的code替换Form1.cs中:
使用系统;
使用System.Collections.Generic;
使用System.Windows.Forms的;命名空间PagedDataGridView
{
公共部分Form1类:表格
{
私人const int的总记录= 43;
私人const int的的pageSize = 10; 公共Form1中()
{
的InitializeComponent();
dataGridView1.Columns.Add(新DataGridViewTextBoxColumn {DataPropertyName =索引});
bindingNavigator1.BindingSource = bindingSource1;
bindingSource1.CurrentChanged + =新System.EventHandler(bindingSource1_CurrentChanged);
bindingSource1.DataSource =新PageOffsetList();
} 私人无效bindingSource1_CurrentChanged(对象发件人,EventArgs的发送)
{
//所需的页面发生了变化,所以取的使用当前记录的页面偏移
INT偏移=(INT)bindingSource1.Current;
VAR记录=新的List&LT;记录和GT;();
的for(int i =偏移; I&LT;胶印+ pageSize的&放大器;&安培; I&LT;总记录;我++)
records.Add(新记录{指数= I});
dataGridView1.DataSource =记录;
} 类记录
{
公众诠释指数{搞定;组; }
} 类PageOffsetList:System.ComponentModel.IListSource
{
公共BOOL ContainsListCollection {搞定;保护套; } 公共System.Collections.IList的GetList()
{
//返回根据总记录和pageSize的页面偏移名单
VAR pageOffsets =新的List&LT; INT&GT;();
对于(INT偏移= 0;偏移LT;总记录;胶印+ =的pageSize)
pageOffsets.Add(偏移);
返回pageOffsets;
}
}
}
}
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.
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:
- Create a Windows Forms application
- Drag onto the form a BindingNavigator, a DataGridView, and a BindingSource
- Replace Form1.cs with the following code:
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屋!