2012-09-25 49 views
0

我已经设置了我的Gridview允许分页。GridView消失没有任何错误

 try 
     { 

      SqlConnection sqlConnection = new SqlConnection("Data Source=JACKCONNECTION\\SQLEXPRESS;Initial Catalog=testbase;Integrated Security=True"); 
      SqlCommand sqlCommand = new SqlCommand(allitemsselectedsqlsrc, sqlConnection); 
      sqlCommand.CommandType = System.Data.CommandType.Text; 
      sqlConnection.Open(); 

      SqlDataAdapter da = new SqlDataAdapter(sqlCommand); 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 

      ArrayList ArrList = new ArrayList(); 

      foreach (DataRow dr in ds.Tables[0].Rows) 
      { 
       ArrList.Add(dr); 
      } 
      GridViewMass.DataSource = ds; 
      GridViewMass.DataBind(); 


     } 
     catch (Exception err) 
     { 
      LabelSelErr.Text = err.Message; 
     } 

另外,我有一个动作PageIndexChanging为GridView控件如下:

protected void gvm_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
     GridViewMass.PageIndex = e.NewPageIndex; 
     GridViewMass.DataBind(); 

    } 

最后,aspx文件包含在GridView如下:

<asp:GridView ID="GridViewMass" runat="server" AllowPaging="True" 
          AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" 
          BorderWidth="1px" CellPadding="4" EnableSortingAndPagingCallbacks="True" 
          ForeColor="Black" GridLines="Horizontal" 
          onpageindexchanging="gvm_PageIndexChanging"> 
          <FooterStyle BackColor="#CCCC99" ForeColor="Black" /> 
          <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" /> 
          <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" /> 
          <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" /> 
          <SortedAscendingCellStyle BackColor="#F7F7F7" /> 
          <SortedAscendingHeaderStyle BackColor="#4B4B4B" /> 
          <SortedDescendingCellStyle BackColor="#E5E5E5" /> 
          <SortedDescendingHeaderStyle BackColor="#242121" /> 
         </asp:GridView> 

奇怪的事情发生是当我点击页面(无论是第二页,第三页还是可以点击的任何页面)时,Gridview GridViewMass就会消失。

我错了吗?以前,我遇到以下错误信息并解决了它们,但现在,我终于得到了一些我无法继续的东西。

  1. “System.InvalidOperationException”类型的第一次机会异常出现在System.dll中
  2. 已经有与此命令必须先关闭相关联的打开的DataReader。
  3. 数据源不支持服务器端数据分页。
  4. GridView'GridView'激发了未处理的事件PageIndexChanging。

感谢任何可以帮助我恢复GridView的帮助。

回答

1

你缺少一样东西在你PageIndexChanging事件

protected void gvm_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    // Here you missing to give datasource to your Grid... 
    GridViewMass.PageIndex = e.NewPageIndex; 
    GridViewMass.DataBind(); 

} 

在你的try/catch块,你必须在此数据集中存储在ViewState

try 
    { 

     SqlConnection sqlConnection = new SqlConnection("Data Source=JACKCONNECTION\\SQLEXPRESS;Initial Catalog=testbase;Integrated Security=True"); 
     SqlCommand sqlCommand = new SqlCommand(allitemsselectedsqlsrc, sqlConnection); 
     sqlCommand.CommandType = System.Data.CommandType.Text; 
     sqlConnection.Open(); 

     SqlDataAdapter da = new SqlDataAdapter(sqlCommand); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 

     ArrayList ArrList = new ArrayList(); 

     foreach (DataRow dr in ds.Tables[0].Rows) 
     { 
      ArrList.Add(dr); 
     } 
     ViewState["DataSource"] = ds; 
     GridViewMass.DataSource = ds; 
     GridViewMass.DataBind(); 


    } 
    catch (Exception err) 
    { 
     LabelSelErr.Text = err.Message; 
    } 

现在这样

pageIndexChangeing变化
protected void gvm_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    // Here you missing to give datasource to your Grid... 
    GridViewMass.DataSource = (DataSet)(ViewState["DataSource"]); 
    GridViewMass.PageIndex = e.NewPageIndex; 
    GridViewMass.DataBind(); 

} 

还记得呃把EnableSortingAndPagingCallbacks设置为False

+0

而且您还使一个方法fillGrid()和pageindexchanging您只是调用该方法 –

+0

我希望接受您的答案,但您的代码无法正常工作。我甚至尝试将'ViewState [“DataSourse”] = ds.Copy();'ViewState [“DataSource”] = ds.Copy();''和'GridViewMass.DataSource =(DataSet)(ViewState [“Data “]);''GridViewMass.DataSource =(DataSet)(ViewState [”DataSource“]);'但它仍然无法工作。 – Jack

+1

我认识到你需要将EnableSortingAndPagingCallbacks设置为False,并将它与你的答案一起使之起作用 – Jack

相关问题