工作需要,要实现 windows 应用程序中用的 datagrid 具有分页功能,不知道 ms 怎么想的, asp.net 的 datagrid 有这样的功能,为什么不在 winForm 的 datagrid 里面提供这样的功能,还得让我费这么大劲儿来重写这个控件,真是麻烦。
首先,我们要做一个类来继承系统的 datagrid:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace oilx.PagingDataGrid
{
public class PagingDataGrid : DataGrid
{
// 声明一些必要的全局变量。
private System.ComponentModel.Container components = null ;
/// ** **
1<summary>
2
3///
4
5/// ** ** </summary>
private const int MarkerWith = 14;
/// ** **
1<summary>
2
3/// ** **
4
5/// ** ** </summary>
private Point _pointTopLeft;
/// ** **
1<summary>
2
3/// ** ** **
4
5**
6
7/// ** ** </summary>
private int _row = 1;
//
private int _rowNumberFigure = 0;
private bool _zeroPadding = false ;
/// ** **
1<summary>
2
3/// ** **
4
5/// ** ** </summary>
private string _rowHeaderCaption = String.Empty;
/// ** **
1<summary>
2
3///
4
5/// ** ** </summary>
private int _pageSize = 10;
/// ** **
1<summary>
2
3/// ** **
4
5/// ** ** </summary>
private int _currentPageCount = 1;
/// ** **
1<summary>
2
3/// ** **
4
5/// ** ** </summary>
private bool _allowPaging = false ;
/// ** **
1<summary>
2
3/// ** **
4
5/// ** ** </summary>
private int _previousPageCount = -1;
/// ** **
1<summary>
2
3/// ** **
4
5/// ** ** </summary>
private DataTable _pageDataSource;
/// ** **
1<summary>
2
3/// ** 构造函数 **
4
5/// ** ** </summary>
/// ** **
1<param name="container"/>
public PagingDataGrid (System.ComponentModel.IContainer container)
{
container.Add( this );
InitializeComponent();
InitiarizeControl();
}
/// ** **
1<summary>
2
3/// ** 构造函数重载 **
4
5/// ** ** </summary>
public FixdItemDataGrid()
{
InitializeComponent();
InitiarizeControl();
}
// 下面我们需要增加一些用于分页的公共属性
/// ** **
1<summary>
2
3/// ** ** ** 页面大小,即每页现实的数据条数 **
4
5/// ** ** </summary>
[Browsable( true ),Description(" ** 页面大小 ** ")]
public int PageSize
{
get
{
return _pageSize;
}
set
{
_pageSize = value ;
}
}
/// ** **
1<summary>
2
3/// ** ** ** 分页数 **
4
5/// ** ** </summary>
[Browsable( true ),Description(" ** 分页数 ** ")]
public int PageCount
{
get
{
if (DataSource == null )
{
return 0;
}
if (_pageDataSource == null || _pageDataSource.Rows.Count <= 0)
{
return 0;
}
int i = _pageDataSource.Rows.Count%PageSize==0?0:1;
return _pageDataSource.Rows.Count/PageSize+i;
}
}
/// ** **
1<summary>
2
3/// ** ** ** 当前页数 **
4
5/// ** ** </summary>
[Browsable( true ),Description(" ** 当前页数 ** ")]
public int CurrentPageCount
{
get
{
if (_currentPageCount < 0)
{
return 1;
}
if (_currentPageCount >= PageCount)
{
return PageCount;
}
return _currentPageCount;
}
set
{
if ( value >=PageCount)
{
_currentPageCount = PageCount;
}
_currentPageCount = value ;
}
}
/// ** **
1<summary>
2
3/// ** 是否允许 **</summary>