【发布时间】:2014-03-05 17:53:58
【问题描述】:
我的应用程序上有一个网格视图(Windows 窗体)。数据来自网络服务调用。有时数据很大,需要一段时间。所以我想添加一些功能,如页面。所以用户可以点击首页、上一页、下一页、最后一页。我知道在 asp.net 中有这样的控制。想知道 windows 窗体,是否有类似的控件可用?如果没有,我必须自己编写代码,谢谢
【问题讨论】:
我的应用程序上有一个网格视图(Windows 窗体)。数据来自网络服务调用。有时数据很大,需要一段时间。所以我想添加一些功能,如页面。所以用户可以点击首页、上一页、下一页、最后一页。我知道在 asp.net 中有这样的控制。想知道 windows 窗体,是否有类似的控件可用?如果没有,我必须自己编写代码,谢谢
【问题讨论】:
当用户点击“下一页”按钮时,在事件 bindingSource1_CurrentChanged 和你的代码可以绑定记录
将 BindingNavigator、DataGridView 和 BindingSource 拖到窗体上
namespace PagedView
{
public partial class Form1 : Form
{
private const int totalRecords = 40;
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)
{
// 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;
}
}
}
}
或点击此链接
【讨论】: