使用DataGrid時自帶的分頁實現起來雖然比較方便
實現的關鍵是設置AllowCustomPaging屬性位True
這裡使用的數據源還是假設為Northwind的Customers表
下面是訪問單頁的存儲過程
CREATE PROCEDURE [GetCustomersDataPage]
@PageIndex INT
@PageSize INT
@RecordCount INT OUT
@PageCount INT OUT
AS
SELECT @RecordCount = COUNT(*) FROM Customers
SET @PageCount = CEILING(@RecordCount *
DECLARE @SQLSTR NVARCHAR(
IF @PageIndex =
SET @SQLSTR =N
ELSE IF @PageIndex = @PageCount
SET @SQLSTR =N
ELSE
SET @SQLSTR =N
EXEC (@SQLSTR)
GO
獲取記錄數和頁數都采用存儲過程的輸出參數
獲取數據源
先定義了連個數據成員
private int pageCount;//頁數
private int recordCount;//記錄數
//獲取單頁數據
private static DataSet GetCustomersData(int pageIndex
{
string connString = ConfigurationSettings
SqlConnection conn = new SqlConnection(connString);
SqlCommand comm = new SqlCommand(
comm
comm
comm
comm
comm
comm
comm
comm
comm
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
dataAdapter
recordCount = (int)comm
pageCount = (int)comm
return ds;
}
//綁定數據到DataGrid
private void DataGridDataBind()
{
DataSet ds = GetCustomersData(PageIndex
this
this
this
}
下面是分頁的幾個變量屬性
public int PageCount
{
get{return this
}
public int PageSize
{
get{return this
}
public int PageIndex
{
get{return this
set{this
}
public int RecordCount
{
get{return recordCount;}
}
注冊DataGrid分頁事件
//分頁事件處理
private void DataGrid
{
DataGrid dg = (DataGrid)source;
dg
DataGridDataBind();
}
最好判斷當前頁面是否是第一次加載
private void Page_Load(object sender
{
if(!Page
{
DataGridDataBind();
}
}
顯示界面如下
這個例子中沒有顯示分頁的一些參數
From:http://tw.wingwit.com/Article/program/net/201311/11473.html