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);
}
}