0

我已将 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();

    }

最后,包含 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>

奇怪的是,当我点击页面时(无论是第二页、第三页还是我可以点击的任何页面),GridviewGridViewMass都会消失。

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

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

感谢任何可以帮助我找回 GridView 的帮助。

4

1 回答 1

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

}

还记得设置你EnableSortingAndPagingCallbacksFalse

于 2012-09-25T07:53:36.103 回答