从数据集中在gridview中分页

本文关键字:gridview 分页 集中 数据 数据集 | 更新日期: 2023-09-27 18:29:08

我有一个保存到viewstate中的数据集,我想对该数据集进行分页。

绑定网格视图的代码

DataSet _ds = _fOrderInvoice.ExecuteDataSet();
ViewState["FIOrders"] = _ds.Tables[0];
grdInvoiced.DataSource = _ds;  // bind the gridview 

现在我想在网格视图上分页。那么,我如何从我保存到ViewState["FIOrders"]中的数据集中做到这一点呢。我的网格视图低于

     <asp:GridView  ID="grdInvoiced" runat="server" Width="100%" ViewStateMode="Enabled"  DefaultSortColumnName="OrderNo"                                        OnSelectedIndexChanged="GridInvoice_SelectedIndexChanged" AutoGenerateColumns="False" OnSorting="invoice_sorting" OnRowDataBound="grdInvoice_Rowdatabount" ShowFooter="true" PageSize="20" AllowPaging="True" AllowSorting="True" DataKeyNames="OrderNo" ExcelExportFileName="Export_AccountTerms.xls"
 ShowHeaderWhenEmpty="true" OnPageIndexChanging="grdInvoiced_PageIndexChanging">
 <Columns>
    <asp:CommandField ShowSelectButton="True" SelectText="View" />
        <asp:TemplateField>
          <ItemTemplate>
         <asp:CheckBox ID="rbtnSelect" GroupName="a" runat="server" />
         <asp:Literal ID="ltOrder" runat="server" Text='<%# Eval("OrderNo")%>' Visible="false"></asp:Literal>
         </ItemTemplate>
       <HeaderStyle HorizontalAlign="Left" Width="50px" />
     </asp:TemplateField>    
     </Columns>    
    </asp:GridView>

PageIndexChanging事件

protected void grdInvoiced_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdInvoiced.PageIndex = e.NewPageIndex+1;
        if (ViewState["FOrders"] != null)
        {
           DataTable ds = (DataTable)ViewState["FIOrders"];
            grdInvoiced.PageIndex = e.NewPageIndex + 1;
            // Collection<FinalizedOrderInvoiceRows> _rows = ViewState["FIOrders"] as Collection<FinalizedOrderInvoiceRows>;
            grdInvoiced.DataSource = ds;
            grdInvoiced.DataBind();
        }
    }

从数据集中在gridview中分页

ViewState转换为DataSet,将Bind转换为GridView

protected void grdInvoiced_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdInvoiced.PageIndex = e.NewPageIndex;
        if (ViewState["FOrders"] != null)
        {
            DataSet ds =(DataSet) ViewState["FIOrders"] ;
            grdInvoiced.DataSource = ds;
            grdInvoiced.DataBind();
        }
    }