我的DataGrid的数据源是SqlDataReader,DataGrid自定义分页已经设置了VirtualItemCount,为什么点击分页按钮的时候页面没有显示啊?
<asp:datagrid id=DataGrid1 style="Z-INDEX: 119; LEFT: 160px; POSITION: absolute; TOP: 232px" runat="server" Width="700px" DataSource="<%# dreader %>" AllowPaging="True" BorderColor="#6595D6" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" PageSize="6" AllowCustomPaging="True" OnPageIndexChanged="DataGrid1_PageIndexChanged" OnSelectedIndexChanged="DataGrid1_SelectedIndexChanged">
<FooterStyle ForeColor="Black" BackColor="#6595D6"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="Black" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="Black" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#6595D6"></HeaderStyle>
<PagerStyle NextPageText="下一页" PrevPageText="上一页" HorizontalAlign="Left" ForeColor="White" BackColor="#6595D6"
Mode="NumericPages"></PagerStyle>
</asp:datagrid>
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);public void  DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
sqlConnection1.Open();
dreader3 = sqlCommand3.ExecuteReader(CommandBehavior.SingleRow);
if (dreader3.Read())
{
DataGrid1.VirtualItemCount = (int)dreader3["mycount"];
}
dreader3.Close();
startIndex = e.NewPageIndex * DataGrid1.PageSize;
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
sqlConnection1.Close();
}public void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}

解决方案 »

  1.   

    要做分页必须用DataSet DataTable DataView 等类似的数据源,用SqlDataReader是不能分页的
      

  2.   

    严重支持
    用dataset要好多了
      

  3.   

    AllowCustomPaging="False" 才行。因爲你是要用DataGrid自帶的分頁,而不是自己寫分頁過程。
      

  4.   

    这是本人曾经写的自定义分页的部分代码
    //业务外观层
    public void Init_Material()
    {
      System.Web.UI.WebControls.DataGrid dataGrid;  dataGrid = this.DataGrid_MaterialBrows;

      virtualItemCount = new MaterialManagementSystem().GetMaterialCount(参数省略);
      if(virtualItemCount<1) return;
      dataGrid.VirtualItemCount =  virtualItemCount ;
      startRecorder = dataGrid.PageSize * dataGrid.CurrentPageIndex;//DataGrid显示的起始行。
      maxRecorders = dataGrid.PageSize;//每页显示的最大行数

      this.MaterialData = new MaterialManagementSystem().GetMaterial(其它参数省略,startRecorder,maxRecorders);//MaterialData是一个DataSet
      this.DataGrid_MaterialBrows.DataBind();
      DataGrid_ShowStats(dataGrid);
    }//数据访问层private MaterialManagementData FillMaterialManagementData(String commandText,int startRecorder,int maxRecorders)
    {
              
      MaterialManagementData   data    = new MaterialManagementData();
      dsCommand.SelectCommand = new SqlCommand();
      dsCommand.SelectCommand.Connection  = new SqlConnection(省略);

      SqlCommand command = dsCommand.SelectCommand;  command.CommandText = commandText;
      command.CommandType = CommandType.StoredProcedure; // use stored proc for perf

      dsCommand.Fill(data,startRecorder,maxRecorders,MaterialManagementData.MaterialManagement_TABLE);//private SqlDataAdapter dsCommand;
      return data;
    }
    如有不懂,本人邮箱[email protected] 再联系。
      

  5.   

    如果AllowCustomPaging="False",IE报错啊,“当 AllowPaging 设置为真并且选定的数据源不实现 ICollection 时,AllowCustomPaging 必须为真,并且 ID 为 DataGrid1 的 DataGrid 必须设置 VirtualItemCount。”