0

我有这个网格视图:

 <asp:GridView ID="GridViewCosts" runat="server"   CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="True" ShowHeaderWhenEmpty="True" AutoGenerateColumns="False" OnRowDeleting="GridViewCosts_RowDeleting" Width="387px" OnSelectedIndexChanged="GridViewCosts_SelectedIndexChanged" AllowPaging="true" AllowSorting="true" 
PageSize="5" >

但这给了我错误:The data source does not support server-side data paging.

在后面的代码中我有这样的东西:

GridViewCosts.DataSource = rdr;
Line 55:                    GridViewCosts.DataBind();

如何分页我没有数据源的gridView

更新 我修改我有这个

 <asp:GridView ID="GridViewCosts" runat="server"   CellPadding="4" 
        ForeColor="#333333" GridLines="None" ShowFooter="True" ShowHeaderWhenEmpty="True"
         AutoGenerateColumns="False" OnRowDeleting="GridViewCosts_RowDeleting" Width="387px"
         OnSelectedIndexChanged="GridViewCosts_SelectedIndexChanged" 
         OnPageIndexChanging="GridViewCosts_PageIndexChanging"

        PageSize="5" >

在代码后面有这个:

  public void getViewProfit()
        {
            string connectionString = cs.getConnection();
            string query = "select Id ,name,value,Description,DateCreate from AllCostView where IdUser = '" + cui.getCurrentId() + "'";
            using (SqlConnection myConnection = new SqlConnection(connectionString))
            {
                myConnection.Open();
                SqlCommand command = new SqlCommand(query, myConnection);
                using (SqlDataReader rdr = command.ExecuteReader())
                {
                   GridViewCosts.DataSource = rdr;
                   GridViewCosts.DataBind();
                }

            }
        }

  protected void GridViewCosts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewCosts.PageIndex = e.NewPageIndex;
            getViewProfit();
        }

没有错误了,但仍然不显示分页

4

1 回答 1

1

如果您使用 datareader?是的,请使用 DataSet/DataTable 代替,因为 Datareader。由于 Datareader 不支持分页和排序功能,DataReader 从数据库中检索 ReadOnly 、 ForwardOnly 数据流。

而你错过了 gridview 的 pageindexChanging 属性,

见下文

<asp:GridView ID="GridViewCosts" runat="server"   CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="True" ShowHeaderWhenEmpty="True" AutoGenerateColumns="False" OnRowDeleting="GridViewCosts_RowDeleting" Width="387px" OnSelectedIndexChanged="GridViewCosts_SelectedIndexChanged" AllowPaging="true" AllowSorting="true" 
PageSize="5" **OnPageIndexChanging="GridViewCosts_PageIndexChanging"**>

后面的代码:

protected void GridViewCosts1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridViewCosts.PageIndex = e.NewPageIndex;

    //rebind your gridview .    

}
于 2013-04-09T19:45:12.873 回答