采用Datagrid属性生成器中的分页方式,我们都会感觉到方便的欣喜。但是,用上面所说的方法,每次页切换时,都会用SELECT * FROM YOURTABLE这样的SQl语句去提取出全部的数据,而没有任何与页有关的筛选器。这大大降低了我们Web程序的效率,也增加了服务器的负担。Datagrid提供的分页特点也变得名不副实。我们希望分页能真正的实现减少每次下载的数据量的功能,就要写代码控制住每次提取的数据量大小。要使用自定义分页,就要将 AllowCustomPaging 属性设置为 true。然后基于 PageSize 和 VirtualItemCount 属性的值计算要显示 DataGrid 控件中每一项所需的页数。PageSize表示的是在 DataGrid 控件的单页上显示的项数,默认值为10。VirtualItemCount表示的是总数目。那么页数自然是VirtualItemCount/PageSize(1)       当然是首先要知道一共有多少条数据,然后我们才好进行分配,写在Page_load里:if (!IsPostBack){StartIndex = 0;//StartIndex是int类型的公用变量SqlConnection MyConnection = new SqlConnection(YourOwnConnectionString);SqlCommand MyCommand = new SqlCommand("SELECT mycount = COUNT(*) FROM Table",MyConnection);MyConnection.Open();SqlDataReader dr = MyCommand.ExecuteReader(CommandBehavior.SingleRow);if (dr.Read())DataGrid1.VirtualItemCount = (int)dr["mycount"];dr.Close();MyConnection.Close();SetGridSource(StartIndex, "上一页");//看看现在的形参有什么不同哦}注:这里说一个本文研究范围之外的知识点,就是上面dr的赋值代码中,我们用的是MyCommand.ExecuteReader(CommandBehavior.SingleRow),而大家一般用的只是MyCommand.ExecuteReader(),但请记住,如果返回记录集只有一行的话,用CommandBehavior.SingleRow进行标识可以提高应用程序的执行效率。这是因为不注明的一般情况下,MyCommand执行是使用 IRowset 接口。而注明了之后,就会利用IRow 接口(如果可用)执行绑定。其中的差别请关注笔者的后续作品。(2)         现在的主要变化当然在重新绑定数据源的方法SetGridSource中。我们给该方法加入两个输入参数:当前的页面、发出控制(即判断用户点击的是“上一页”还是“下一页”等按钮)。下面的代码主要实现按钮型导航栏,有两个按钮“上一页”与“下一页”。“第一页”与“最后一页”的写法,请网友们自行实现。在如下示例中,column1为数据表之主键。每页有5行数据。private void SetGridSource (int StartPosition, string GoToPage)
{
 SqlConnection MyConnection = new  SqlConnection(YourOwnConnectionString);
 SqlCommand MyCommand = null;
 switch (GoToPage)
 {
  case "上一页":
      MyCommand = new 
         SqlCommand("SELECT TOP 5 * FROM Table WHERE column1 >= @ID ORDER BY column1",MyConnection);
      if (StartPosition == 0)
        MyCommand.Parameters.Add("@ID",SqlDbType.NVarChar, 10).Value = "";//这里参数10是ID字段的长度
      else
        MyCommand.Parameters.Add("@ID",SqlDbType.NVarChar,10).Value =
                ViewState[(DataGrid1.CurrentPageIndex + 1).ToString()];break;
  case "下一页":
      MyCommand = new 
         SqlCommand("SELECT TOP 5 * FROM Table WHERE column1 > @ID ORDER BY column1",MyConnection);//注意:这里用的是>,不是>=哟
      MyCommand.Parameters.Add("@ID", SqlDbType.NVarChar,10).Value = DataGrid1.Items[4].Cells[0].Text;//Items[4]表示第5行即每页显示的最后一行
      break;
 }
 MyConnection.Open();
 SqlDataReader dr = MyCommand.ExecuteReader();
 DataGrid1.DataSource = dr;
 DataGrid1.DataBind();
 dr.Close();
 MyConnection.Close();  //用ViewState来缓存刚才访问过的那一页的第一行中的主键值
 ViewState[(DataGrid1.CurrentPageIndex + 1).ToString()]=DataGrid1.Items[0].Cells[0].Text;
}

解决方案 »

  1.   

    参考:
    http://eu.webmatrixhosting.net/Webdiyer/
      

  2.   

    seeASP.NET DataGrid Paging - Custom Paging w/ Caching & Numeric Links
    http://www.dotnetjunkies.com/Tutorial/ShowContent.aspx?cg=07FB766A-35F2-4691-923A-D5F1BA3A12DD&forumID=4073
    Custom ASP.NET Datagrid Paging With Exact Count 
    http://www.4guysfromrolla.com/webtech/082901-1.shtmlImplement Custom Paging in the ASP.Net Datagrid Control 
    http://www.dotnetjohn.com/articles/articleid28.aspxCustom Paging User Control for ASP.NET DataGrid
    http://www.freevbcode.com/ShowCode.Asp?ID=5574
      

  3.   

    我是初学者,我也定义过“首页”“上一页”“下一页”“末页”,还是借助datagrid显示效率不高,我是使用linkbutton实现的,这样就出现了一个问题,怎样把这些linkbutton加到页脚上,直接写就可以吗?我会好好研究一下你写的,又不明白的地方会及时问你:P)
      

  4.   

    以上的方案基本上是单独的分页控件,没有与DATAGRID整合在一起