DataGrid 分页必须考虑的几个问题

本文介绍ASP.NET中DataGrid组件的分页处理方法,包括何时启用分页、删除记录后的页码调整、查询时的CurrentPageIndex设置及分页显示逻辑。提供了实用的代码示例和一个封装好的类来简化DataGrid的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值