using System;
using System.Windows.Forms;
using System.Data;namespace MyDataGridControls
{
public class MyDataGrid : DataGrid
{
private void InitializeComponent()
{
((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this)).EndInit();
}
 
public MyDataGrid()
{
} /// 总页数
private int pageCount = 1;
/// <summary>
/// 每页行数
/// </summary>
private int pageRows = 35;
/// <summary>
/// 总行数
/// </summary>
private int rowCount = 1;
/// <summary>
/// 当前页
/// </summary>
private int curentPageIndex = 1;
/// <summary>
/// 能否前进
/// </summary>
private bool canPri = false;
/// <summary>
/// 能否后退
/// </summary>
private bool canNext = false;
/// <summary>
/// 能否到第一页
/// </summary>
private bool canFirst = false;
/// <summary>
/// 能否到最后一页
/// </summary>
private bool canLast = false;
/// <summary>
/// 设置返回记录集种类
/// </summary>
private int ReDataSort = 1; private DataTable Build_dt = new DataTable();
private DataView  Build_dv  = new DataView();
private DataSet   Build_ds   = new DataSet(); /// <summary>
/// 得到数据源
/// </summary>
private void BindSource(DataTable dt)
{
this.Build_dt = dt;
this.rowCount = dt.Rows.Count;
this.pageCount = rowCount / pageRows;
if(rowCount % pageRows > 0)
{
this.pageCount = this.pageCount + 1;
}   
BuildTable();
} public void SetSource(DataTable dt)
{
ReDataSort = 1;
BindSource(dt);
} public void SetSource(DataView dv)
{
ReDataSort = 2;
Build_dv = dv;
BindSource(dv.Table);
} public void SetSource(DataSet ds,string TableName)
{
ReDataSort = 3;
Build_ds = ds;
BindSource(ds.Tables[TableName]);
} public void SetSource(DataSet ds)
{
ReDataSort = 3;
Build_ds = ds;
BindSource(ds.Tables[0]);
} //绑定记录
private void BuildTable()
{
if( this.curentPageIndex < 1)
{
this.curentPageIndex = 1;
}
//当前页面超出总页数..返回第一页
if( this.curentPageIndex > this.pageCount)
{
this.curentPageIndex = 1;
} DataTable tempTable = this.Build_dt.Copy();
tempTable.Clear(); int startRows =( curentPageIndex - 1) * pageRows;
int endRows = curentPageIndex * pageRows;
if(endRows > this.rowCount)
{
endRows = this.rowCount;
}
for(int i=startRows;i<endRows;i++)
{
DataRow row = tempTable.NewRow();
row.ItemArray = ((DataRow)(this.Build_dt.Rows[i])).ItemArray;
tempTable.Rows.Add(row);
}
//用于返回时DataSource只返回当前页有记录
switch (ReDataSort)
{
case 1:
this.DataSource = tempTable;
break;
case 2:
this.DataSource = tempTable.DefaultView;
break;
default:
DataSet ds = new DataSet();
ds.Tables.Add(tempTable);
this.DataSource = tempTable;
break;
}
SetMoveDataBtn();
} /// <summary>
/// 设置能否移动记录
/// </summary>
private void SetMoveDataBtn()
{
// 页面只有一页...
if(pageCount <= 1)
{
PageCount = 1;

this.canFirst = false;
this.canNext = false;
this.canLast = false;
this.canPri = false;
return;
}

// 当前页面在第一页...
if(curentPageIndex == 1)
{
this.canFirst = false;
this.canPri = false;
this.canNext = true;
this.canLast = true;
return;
} // 当前页面在最后一页...
if( (curentPageIndex) == pageCount)
{
this.canFirst = true;
this.canPri = true;
this.canNext=false;
this.canLast = false;
}
//当前页面在第一页和最后一页之间...
else 
{
this.canFirst = true;
this.canPri = true;
this.canNext = true;
this.canLast = true;
}
} /// <summary>
/// 第一页
/// </summary>
public void FirstPage()
{
if(!canFirst)
{
return;
} this.curentPageIndex = 1; BuildTable();
} /// <summary>
/// 最后一页
/// </summary>
public void LastPage()
{
if(!canLast)
{
return;
} this.curentPageIndex = pageCount; BuildTable();
} /// <summary>
/// 下一页
/// </summary>
public void NextPage()
{
if(!canNext)
{
return;
} this.curentPageIndex = curentPageIndex + 1; BuildTable();
} /// <summary>
/// 前一页
/// </summary>
public void PriPage()
{
if(!canPri)
{
return;
}
this.canNext=true;
this.curentPageIndex = curentPageIndex - 1;
if(curentPageIndex<=1)
{
this.canPri=false;
}
else
{
this.canPri=true;
}
BuildTable();
}

public bool CanPri
{
get
{
return this.canPri;
}
} public bool CanNext
{
get
{
return this.canNext;
}
} public bool CanFirst
{
get
{
return this.canFirst;
}
} public bool CanLast
{
get
{
return this.canLast;
}
} /// <summary>
/// 得到当前页和设置跳转页
/// </summary>
public int CurentPageIndex
{
set 
{
if(curentPageIndex != value)
{
this.curentPageIndex = value;
if(curentPageIndex > pageCount || curentPageIndex < 0)
{
FirstPage();
}
else
{
BuildTable();
}
}
}
get
{
return this.curentPageIndex;
}
} public int RowCount
{
get
{
return this.rowCount;
}
} public int PageCount
{
set 
{
this.pageCount = value;
}
get
{
return this.pageCount;
}
}

/// <summary>
/// 每页记录的个数
/// </summary>
public int PageRows
{
set
{
this.pageRows = value;
}
get
{
return this.pageRows;
}
} /// <summary>
/// 返回调用SetSource绑定时的记录
/// </summary>
public object GetSource
{
get
{
switch (ReDataSort)
{
case 1:
return this.Build_dt;
case 2:
return this.Build_dv;
default:
return this.Build_ds;
}
}
}
}
}

解决方案 »

  1.   

    //调用
    private void Form2_Load(object sender, System.EventArgs e)
    {
    SqlConnection conn = new SqlConnection("server=.;User ID=sa;database=pubs");
    SqlDataAdapter dsCommand = new SqlDataAdapter();
    dsCommand.TableMappings.Add("Table", "employee");
    SqlCommand command = new SqlCommand("select * from employee order by minit",conn);
    dsCommand.SelectCommand = command;
    SqlCommandBuilder commandB = new SqlCommandBuilder(dsCommand);
    conn.Open();
    DataSet ds = new DataSet();
    dsCommand.Fill(ds,"employee");
    conn.Close(); dg.PageRows = 8;
    DataView dv = ds.Tables["employee"].DefaultView;
    dg.SetSource(dv);
    SetBtnEnable();
    }private void SetBtnEnable()
    {
    this.btn_Pri.Enabled = dg.CanPri;
    this.btn_Next.Enabled = dg.CanNext;
    this.btn_First.Enabled = dg.CanFirst;
    this.btn_Last.Enabled = dg.CanLast;
    }private void button5_Click(object sender, System.EventArgs e)
    {
    DataView dv = (DataView)dg.DataSource;  //返回当前页的记录 //DataView dv = (DataView)dg.GetSource; //返回dg绑定时的全部记录
    }private void button6_Click(object sender, System.EventArgs e)
    {
    //直接跳到转X页
    dg.CurentPageIndex = int.Parse(textBox1.Text.Trim());
    SetBtnEnable();
    }private void btn_First_Click(object sender, System.EventArgs e)
    {
    this.dg.FirstPage();
    SetBtnEnable();
    }private void btn_Pri_Click(object sender, System.EventArgs e)
    {
    this.dg.PriPage();
    SetBtnEnable();
    }private void btn_Next_Click(object sender, System.EventArgs e)
    {
    this.dg.NextPage();
    SetBtnEnable();
    }private void btn_Last_Click(object sender, System.EventArgs e)
    {
    this.dg.LastPage();
    SetBtnEnable();
    }
    望大家多多提意见...本人还是菜鸟
      

  2.   

    使用 Visual C# .NET 对 DataGrid Windows 控件执行分页
    http://support.microsoft.com/default.aspx?scid=kb;zh-cn;307710