如何在分页发生之前从GridView中获取所有数据

how to get all data from GridView before the paging occurs

我有 GridViewSqlDataSource 作为数据源。 GridViewPageSize 属性 设置为 10,AllowPaging 设置为 true。我想要实现的是从 SqlDataSource.

获取所有行数据

我试图在 Page_PreRender 上获取数据,但我在 gridview 上得到的行数为 0。 gridview中实际有100行。

这是当前代码:

protected void Page_PreRender(object sender, EventArgs e)
    {
        gvBills.AllowPaging = false;
        int t = gvBills.Rows.Count;
        gvBills.AllowPaging = true;
    }

关于如何从 gridview 获取所有数据的任何建议或指导都会有所帮助。

更新

当我使用

protected void gvBills_PreRender(object sender, EventArgs e)
    {
        gvBills.AllowPaging = false;
        int t = gvBills.Rows.Count;
        gvBills.AllowPaging = true;
        gvBills.PageSize = 350;
    }

我得到了正确的行数,但 gridview 在一页中显示了所有行。

这是我的 gridview 控件

 <asp:GridView runat="server" ID="gvBills" CssClass="gridtable" PagerStyle-CssClass="pgr" DataSourceID="DataSourceBills" 
        AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="false" ShowFooter="false" AllowSorting="true"
        OnRowDataBound="gvBills_RowDataBound" EmptyDataText="<%$ Resources:general_tags, NoDataFound %>" AllowPaging="true" PageSize="350">

和我的 sqldatasource

<asp:SqlDataSource ID="DataSourceBills" runat="server" ConnectionString="<%$ ConnectionStrings:EnergyManagementConnectionString %>"
        SelectCommand="usp_BCPD_SearchBillsHistory" SelectCommandType="StoredProcedure"
        DeleteCommand="usp_BCPD_DeleteData" DeleteCommandType="StoredProcedure">
        <SelectParameters>           
            <asp:SessionParameter Name="UD_ID" SessionField="UserID" Type="Int32" />
            <asp:ControlParameter Name="BillType" ControlID="hfld_BillType" PropertyName="Value" Type="Int32" />
            <asp:ControlParameter Name="SL_ID" ControlID="hfld_SL_ID" PropertyName="Value" Type="Int32" />                
            <asp:ControlParameter Name="CD_ID" ControlID="ddlConsumer" PropertyName="SelectedValue" Type="Int32" />   
            <asp:ControlParameter Name="CUR_ID" ControlID="ddlCustomer" PropertyName="SelectedValue" Type="Int32" />             
            <asp:ControlParameter Name="BSD_ID" ControlID="hfld_BSD_ID" PropertyName="Value" Type="Int32" />
            <asp:ControlParameter Name="Consumers" ControlID="hfld_ArrFinalConsumersSearch" PropertyName="Value" Type="String" />
            <asp:ControlParameter Name="Channels" ControlID="hfld_ArrFinalChannelsSearch" PropertyName="Value" Type="String" />
            <asp:ControlParameter Name="Customers" ControlID="hfld_ArrFinalCustomersSearch" PropertyName="Value" Type="String" />
            <asp:ControlParameter Name="strFromProductionDate" ControlID="hfld_FromProductionDate" PropertyName="Value" Type="String" />
            <asp:ControlParameter Name="strToProductionDate" ControlID="hfld_ToProductionDate" PropertyName="Value" Type="String" />
            <asp:ControlParameter Name="strProductionDate" ControlID="hfld_ProductionDate" PropertyName="Value" />
            <asp:ControlParameter Name="Reading" ControlID="ddlReadingFlag" PropertyName="SelectedValue" Type="Int32" />
            <asp:ControlParameter Name="strFromReadingDate" ControlID="hfld_FromReadingDate" PropertyName="Value" Type="String" />
            <asp:ControlParameter Name="strToReadingDate" ControlID="hfld_ToReadingDate" PropertyName="Value" Type="String" />
            <asp:ControlParameter Name="BillStatus" ControlID="ddlBillStatus" PropertyName="SelectedValue" Type="Int32" />
            <asp:ControlParameter Name="BPET_ID" ControlID="ddlBillError" PropertyName="SelectedValue" Type="Int32" />
            <asp:ControlParameter Name="CustomerType" ControlID="ddlCustomerType" PropertyName="SelectedValue" Type="Int32" />
            <asp:Parameter Name="ChannelDisplayType" Type="Int32" DefaultValue="0" />
            <asp:ControlParameter Name="AdditionalParameter" ControlID="hfld_AdditionalParameter" PropertyName="Value" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

我会将这样的东西(其中 ds1 是您的 sqldatasource)粘贴到 Page_Load 事件中:

DataView dv = (DataView)ds1.Select(DataSourceSelectArguments.Empty);
DataTable dt = new DataTable();
dt = dv.ToTable();

然后您可以从 'dt' 中获取数据。

更新

如果您想从 Gridview 而不是 sqldatasource 获取数据,那么您可以执行以下操作:

private DataTable getGridData()
{ 
    DataTable dt = new DataTable();
    DataRow dr;
    dt.Columns.Add(new System.Data.DataColumn("RowId", typeof(Int)));
    dt.Columns.Add(new System.Data.DataColumn("Title", typeof(String)));
    //Add more data columns as needed.

    foreach (GridViewRow row in gvBills.Rows)
    {
        dr = dt.NewRow();
        dr[0] = (int)row.FindControl("RowId");
        dr[1] = row.FindControl("Title").ToString();
        //Add more values if more columns are added / needed.

        dt.Rows.Add(dr);
    }

    return dt;
}

从任何需要使用数据的地方调用此方法。

更新 2 为避免仅在当前页面上返回行,就像您之前在其他代码中所做的那样,然后在 运行 通过行之前再次进行数据绑定:

gvBills.Allowpaging = false;
gvBills.DataBind();