asp上的分页:DataGrid不起作用

时间:2015-10-17 23:11:48

标签: c# asp.net datagrid pagination

我正在尝试为我的Gridview添加分页。我已将以下内容添加到属性中:

<table id="myTable" class="table tbody" runat="server" visible="false">
     <tbody>
         <tr>
            <td>
               <asp:DataGrid ID="myGrid" runat="server" CssClass="table table-striped tbody" Visible="false"
                      AutoGenerateColumns="True"
                      AllowPaging="True"
                      AllowCustomPaging="true"
                      ForeColor="black"
                      HeaderStyle-Font-Bold="true"
                      HeaderStyle-ForeColor="black"
                      GridLines="None"
                      EnableViewState="false" />
             </td>
          </tr>
      </tbody>
</table>

结果集返回22行,它正确显示第一页上的前10行,但我没有任何选项可以移动到下一页。没有数字或箭头可以按移动到第2页等。

任何人都可以帮我解决我做错的事吗?

1 个答案:

答案 0 :(得分:0)

我认为您应该删除Allow Custom paging property并根据您的要求设置PageSize属性。然后,您必须将OnPageIndexChanging事件添加到网格中。例如:

AllowPaging="true" PageSize="8" OnPageIndexChanging="stockTakeGrid_PageIndexChanging"

因此,在后面的代码中,您还必须处理上述事件,例如:

        protected void stockTakeGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        stockTakeGrid.DataSource = ViewState["stockDetails"];
        stockTakeGrid.PageIndex = e.NewPageIndex;
        stockTakeGrid.AutoGenerateColumns = false;
        stockTakeGrid.DataBind();
    }

当您最初加载网格时,将页面索引设置为 0