Asp.net中的DataGrid分頁必須考慮以下幾個問題
1。如果資料不多,則不分頁,多則分頁
if(DataGrid1.Items.Count < DataGrid1.PageSize+1)
DataGrid1.AllowPaging = false;
else
{
DataGrid1.AllowPaging = true;
}
DataGrid1.DataBind();
if(DataGrid1.Items.Count < DataGrid1.PageSize+1)
DataGrid1.AllowPaging = false;
else
{
DataGrid1.AllowPaging = true;
}
DataGrid1.DataBind();
但是我現在還不知道為什麼要datebind 2次。如果不這樣處理的話,資料不夠的話,也會顯示頁嗎1。
2。後一頁最後一條記錄删除,需要将他的CurrentPageIndex減一。
if (DataGrid1.Items.Count==1)
if(DataGrid1.CurrentPageIndex !=0)
DataGrid1.CurrentPageIndex-=1;
3。如果頁面上提供了查詢功能時,一定要在DateBind之前把CurrentPageIndex 設為0,否則很有可能出錯,因為有可能查詢前DataGrid的CurrentPageIndex >查詢後DataGrid的PageCount。
4。分頁時,先顯示第一頁,在判斷能不能分頁
DataGrid1.CurrentPageIndex = 0;
DataGrid1.DataBind();
if (e.NewPageIndex < DataGrid1.PageCount )
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}
建了一個類,專門處理datagrid的顯示,分頁等
public class XuDataGrid
{
public XuDataGrid()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public static void BindDeleteData(string StrSql,DataGrid g)
{
if(g.AllowPaging)
{
if (g.Items.Count==1)
if(g.CurrentPageIndex !=0)
g.CurrentPageIndex-=1;
if(!pBindDataDelete(StrSql,g))
BindDataInitial(StrSql,g);
}
else
BindData(StrSql,g);
}
private static bool pBindDataDelete(string StrSql,DataGrid g)
{
try
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
if(g.EditItemIndex !=-1)
g.EditItemIndex =-1;
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
}
catch
{
return false;
}
return true;
}
private static bool pBindDataPage(string StrSql,DataGrid g,int IntNewPage)
{
try
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
if(g.EditItemIndex !=-1)
g.EditItemIndex =-1;
g.CurrentPageIndex =IntNewPage;
g.DataBind();
}
catch
{
return false;
}
return true;
}
private static bool pBindDataEdit(string StrSql,DataGrid g,int IntEditIndex)
{
try
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
g.EditItemIndex =IntEditIndex;
g.DataBind();
}
catch
{
return false;
}
return true;
}
public static void BindData(string StrSql,DataGrid g)
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
g.EditItemIndex =-1;
g.DataBind();
}
//允許分頁 --得到所有資料,初始化狀态,同時要2次邦定
public static void BindDataAllowPaging(string StrSql,DataGrid g)
{
//BindDataInitial(StrSql,g);
g.DataSource = ConnectionManager.GetDataView(StrSql);
g.CurrentPageIndex =0;
g.EditItemIndex =-1;
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
}
public static void BindEditData(string StrSql,int IntEditIndex,DataGrid g)
{
if(g.AllowPaging)
{
if(!pBindDataEdit(StrSql,g,IntEditIndex))
BindDataInitial(StrSql,g);
//iBindEditData(StrSql,IntEditIndex,g.CurrentPageIndex,g);
}
else
iBindEditData(StrSql,IntEditIndex,g);
}
//沒有分頁 --編輯資料
private static void iBindEditData(string StrSql,int IntEditIndex,DataGrid g)
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
if(IntEditIndex < g.Items.Count)
g.EditItemIndex =IntEditIndex;
g.DataBind();
}
public static void BindPageData(string StrSql,int IntPageIndex,DataGrid g)
{
if(!pBindDataPage(StrSql,g,IntPageIndex))
BindDataInitial(StrSql,g);
}
private static void BindDataInitial(string StrSql,DataGrid g)
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
g.CurrentPageIndex =0;
g.EditItemIndex =-1;
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
}
//允許分頁 --得到更新後資料資料
public static void BindUpdateData(string StrSql,DataGrid g)
{
BindEditData(StrSql,-1,g);
}
public static void BindCancelData(string StrSql,DataGrid g)
{
BindEditData(StrSql,-1,g);
}
}