以前收藏的,供参考
//控件
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button btnFirstPage;
private System.Windows.Forms.Button btnNextPage;
private System.Windows.Forms.Button btnPreviousPage;
private System.Windows.Forms.Button btnLastPage;
private System.Windows.Forms.Button btnFillGrid;
private System.Windows.Forms.TextBox txtDisplayPageNo;
private System.Windows.Forms.TextBox txtPageSize;//每页显示行数,需要提供初始值
//控件事件
private void btnFirstPage_Click(object sender, System.EventArgs e)
{
if (CheckFillButton() == false) 
{
return;
} //Check if you are already at the first page.
if (currentPage == 1) 
{
MessageBox.Show("You are at the First Page!");
return;
} currentPage = 1;
recNo = 0;
LoadPage();
} private void btnPreviousPage_Click(object sender, System.EventArgs e)
{
if (CheckFillButton() == false) 
{
return;
} if (currentPage == PageCount) 
{
recNo = pageSize * (currentPage -2);
} currentPage -= 1;
//Check if you are already at the first page.
if (currentPage < 1) 
{
MessageBox.Show("You are at the First Page!");
currentPage = 1;
return;
}
else 
{
recNo = pageSize * (currentPage - 1);
}
LoadPage();
} private void btnNextPage_Click(object sender, System.EventArgs e)
{
//If the user did not click the "Fill Grid" button, then return.
if (CheckFillButton() == false) 
{
return;
} //Check if the user clicks the "Fill Grid" button.
if (pageSize == 0) 
{
MessageBox.Show("Set the Page Size, and then click the Fill Grid button!");
return;
} currentPage += 1;
if (currentPage > PageCount) 
{
currentPage = PageCount;
//Check if you are already at the last page.
if (recNo == maxRec) 
{
MessageBox.Show("You are at the Last Page!");
return;
}
}
LoadPage();
} private void btnLastPage_Click(object sender, System.EventArgs e)
{
if (CheckFillButton() == false) 
{
return;
} //Check if you are already at the last page.
if (recNo == maxRec) 
{
MessageBox.Show("You are at the Last Page!");
return;
}
currentPage = PageCount;
recNo = pageSize * (currentPage - 1);
LoadPage();
} private void btnFillGrid_Click(object sender, System.EventArgs e)
{
// Set the start and max records. 
pageSize = Convert.ToInt32(txtPageSize.Text);
maxRec = dtSource.Rows.Count;
PageCount = maxRec / pageSize; //Adjust the page number if the last page contains a partial page.
if ((maxRec % pageSize) > 0) 
{
PageCount += 1;
} // Initial seeings
currentPage = 1;
recNo = 0; // Display the content of the current page.
LoadPage();
}
//方法
private void LoadPage() 
{

int i;
int startRec;
int endRec;
DataTable dtTemp; //Clone the source table to create a temporary table.
dtTemp = dtSource.Clone(); if (currentPage == PageCount) 
{
endRec = maxRec;
}
else 
{
endRec = pageSize * currentPage;
}
startRec = recNo; //Copy rows from the source table to fill the temporary table.
for (i = startRec; i < endRec; i++) 
{
dtTemp.ImportRow(dtSource.Rows[i]);
recNo += 1;
}
dataGrid1.DataSource = dtTemp;
DisplayPageInfo();
} private void DisplayPageInfo() 
{
txtDisplayPageNo.Text = "Page " + currentPage.ToString() + "/ " + PageCount.ToString();
} private bool CheckFillButton() 
{
//Check if the user clicks the "Fill Grid" button.
if (pageSize == 0) 
{
MessageBox.Show("Set the Page Size, and then click the Fill Grid button!");
return false;
}
else 
{
return true;
}
}