Visual studio 2010 GridView将消失,不会出现任何错误

Visual studio 2010 GridView将消失,不会出现任何错误,visual-studio-2010,sql-server-2008,gridview,Visual Studio 2010,Sql Server 2008,Gridview,我已将Gridview设置为允许分页 try { SqlConnection sqlConnection = new SqlConnection("Data Source=JACKCONNECTION\\SQLEXPRESS;Initial Catalog=testbase;Integrated Security=True"); SqlCommand sqlCommand = new SqlCommand(alli

我已将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;
        }
此外,我还有一个针对GridView的操作
PageIndexChanging
,如下所示:

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

    }
<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的aspx文件如下所示:

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

    }
<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>

奇怪的是,当我点击页面时(无论是第2页、第3页还是我可以点击的任何页面),Gridview
GridViewMass
就会消失

我编码错了吗?以前,我遇到了以下错误消息,并已解决了这些问题,但现在,我遇到了一些无法继续的问题

  • System.dll中发生类型为“System.InvalidOperationException”的第一次意外异常
  • 已存在与此命令关联的打开的datareader,必须先关闭该命令
  • 数据源不支持服务器端数据分页
  • GridView“GridView”触发了未处理的事件PageIndexChanging

  • 感谢任何能帮助我找回GridView的帮助

    您在
    pageIndexchange
    事件中只缺少一件事

    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;
        }
    
    现在在您的
    pageindexchange
    中,像这样更改

    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();
    
    }
    

    还记得将
    启用排序和分页回调设置为
    False

    您还创建了一个方法fillGrid(),在PageIndexchange中,您只需调用该方法即可。我希望接受您的答案,但您的代码不起作用。我甚至尝试更改
    ViewState[“datasource”]=ds.Copy()
    to
    ViewState[“DataSource”]=ds.Copy()
    GridViewMass.DataSource=(数据集)(ViewState[“数据])
    GridViewMass.DataSource=(数据集)(ViewState[“数据源])
    但仍然无法工作。我认识到您需要将
    启用排序和分页回调设置为False,并将您的答案设置为False,以使其正常工作,因此我更新了您的答案并将其标记为正确。你拿到15分了吗?