DataGrid 分页程序

一 传统分页显示程序(datagrid )
  private void PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
  DataSet ds1 = (DataSet) Cache["ECInforDataSet"];
  DataGrid dg = (DataGrid)source;
  dg.DataSource = ds1.Tables[0].DefaultView;
  dg.CurrentPageIndex = e.NewPageIndex;
  this.DataBind();
}

其中Cache["ECInforDataSet"]如下设置:
SqlProcedure sp1 = new SqlProcedure("SelectName2");
  sp1.m_ConnStr="Data Source=localhost;Initial Catalog=hangwu;persist security info=True;user id=sa;password=sa;";
  SqlResult rst1 = sp1.Call(txtSanZiMa.Text.ToString(),txtYuanGongHao.Text.ToString());
  //Response.Write(txtSanZiMa.Text.ToString());
    Cache["ECInforDataSet"] = rst1.dataSet;
//备注——感觉使用Cache有点象使用Session

二 高效分页技术——每次只读几页——而不是一下将全部数据读进来
  其中利用的分页的存储过程见四的8

        //页的大小
  private static int PageSize = 5;

private void Page_Load(object sender, System.EventArgs e)
{
  if (!Page.IsPostBack)
  {
  //显示第一页的记录
  ShowResult(0, PageSize);
  }
}

void ShowResult(int pageIndex, int pageSize)
{
  //绑定Repeater控件
  products.DataSource = BLL.Product.GetProductsByCategory(int.Parse(Request.QueryString["categoryId"]),
      pageSize, pageIndex);
  products.DataBind();
  //调用Product类中的方法获得该类商品的总数
  int resultCount = BLL.Product.GetProductCountByCategory(int.Parse(Request.QueryString["categoryId"]));
  int count;

  //如果查询结果总数是页大小的整数倍
  if (resultCount%PageSize == 0)
  {
  count = resultCount/PageSize;
  PageCount.Text = count.ToString();
  }
  else
  {
  count = resultCount/PageSize+1;
  PageCount.Text = count.ToString();
  }

  page.Items.Clear();
  //绑定页码到DropDownList控件
  for(int i=0; i<count; i++)
  {
  ListItem item = new ListItem((i+1).ToString(), i.ToString());
  page.Items.Add(item);
  }
  page.SelectedIndex = pageIndex;
}

          //分页显示中用到的页面切换——这里用到的DropDownList控件
          private void page_SelectedIndexChanged(object sender, System.EventArgs e)
{
  ShowResult(page.SelectedIndex, PageSize);
}

          //其中用到的BLL.Product.GetProductsByCategory(int.Parse(Request.QueryString["categoryId"]),pageSize, pageIndex);的在如下位置定义:
          BLL中的Product.cs中定义:
          public static SqlDataReader GetProductsByCategory(int categoryId, int pageSize, int pageIndex)
{
  SqlParameter[] para = {
      new SqlParameter("@CategoryId", categoryId),
      new SqlParameter("@pageSize", pageSize),
      new SqlParameter("@pageIndex", pageIndex)
      };
  return SQLHelper.ExecuteReader(SQLHelper.CONN_STRING, CommandType.StoredProcedure, "GetProductByCategory", para);
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值