GridView消失,没有任何错误

时间:2012-09-25 07:06:02

标签: 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(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();

    }

最后,包含Gridview的aspx文件如下:

<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将会消失。

我的代码是错误的吗?以前,我遇到了以下错误消息并解决了它们,但是现在,我最终得到的东西不能再继续了。

  1. System.dll
  2. 中出现'System.InvalidOperationException'类型的第一次机会异常
  3. 已经有一个与此命令关联的打开的datareader,必须先关闭它。
  4. 数据源不支持服务器端数据分页。
  5. GridView'GridView'触发了未处理的事件PageIndexChanging。
  6. 感谢任何帮助我恢复GridView的帮助。

1 个答案:

答案 0 :(得分: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