2013-04-09 40 views
0

我有這個網格視圖:如何分頁的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" AllowPaging="true" AllowSorting="true" 
PageSize="5" > 

但是這給在我的錯誤:The data source does not support server-side data paging.

在後面的代碼我有這樣的事情:

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

如何尋呼我的gridView沒有數據源

UPDATE 我修改我有這個

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

有沒有錯誤更多,但仍然沒有顯示分頁

回答

1

如果您使用DataReader的是,請?使用DataSet/DataTable來代替Datareader。因爲DataReader沒有支持分頁和排序能力,DataReader的檢索數據庫數據的只讀,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 .  

} 
+0

更新我的問題,請參閱它 – Krasimir 2013-04-09 19:55:29

+0

你在頁面loage綁定的GridView的IsPostBack裏面的!如果(!IsPostBack) getViewProfit();如果(!IsPostBack) void page-load() { } } – 2013-04-09 20:05:18