带分页的 DataGridView 扩展版本
顾名思义。在阅读了D Strauss发表的一篇类似文章后,我受到了启发,扩展了DataGridView控件。
引言
这是我在CodeProject上的第一篇文章。这个控件是对Visual Studio .NET自带的DataGridView
控件的一个简单扩展。在有人评论代码优化等方面之前,我可能会在未来更新它。
背景
在阅读了D Straus关于制作DataGridView
分页的文章后,我想将其扩展以包含分页功能。
使用代码
这个控件的使用非常简单。下载源代码,编译DLL,将其导入到VS设计器模式下的工具箱中,就可以开始使用了。
主要方法有:
GoToNextPage()
GoToLastPage()
GoToPreviousPage()
GoToFirstPage()
GoToPageNumber(int pageno)
public void GoToPageNumber(int n)
{
DataSet dsTempSubSet = new DataSet();
DataRow[] rows = new DataRow[RowsPerPage];
if (n == 1)
{
GoToFirstPage();
}
if ((0 < n) && (n <= GetTotalPages()))
{
int PageIndex = (n - 1) * RowsPerPage;
if (PageIndex >= dsTemp.Tables[0].Rows.Count)
{
GoToFirstPage();
}
else
{
int WholePages = dsTemp.Tables[0].Rows.Count / RowsPerPage;
if ((dsTemp.Tables[0].Rows.Count % RowsPerPage) != 0 && n == GetTotalPages())
{
rows = new DataRow[dsTemp.Tables[0].Rows.Count -
(WholePages * RowsPerPage)];
}
for (int i = 0, i2 = PageIndex; i < rows.Length && i2 <
dsTemp.Tables[0].Rows.Count; i2++, i++)
{
rows[i] = dsTemp.Tables[0].Rows[i2];
}
dsTempSubSet.Merge(rows);
bindingSource1.DataSource = dsTempSubSet.Tables[0];
this.DataSource = bindingSource1;
CurrentPage = n;
}
}
}
历史
- 1.1 - 2007年6月21日:优化了代码,现在大多数导航方法都使用
GoToPageNumber(int)
。 - 1.0 - 2007年6月20日:初始上传。