我GridView
有一个SqlDataSource
作为DataSource。 GridView
将PageSize
属性设置为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>
答案 0 :(得分:1)
我会把这样的东西(其中ds1是你的sqldatasource)粘贴到Page_Load事件中:
DataView dv = (DataView)ds1.Select(DataSourceSelectArguments.Empty);
DataTable dt = new DataTable();
dt = dv.ToTable();
然后,您可以从&#39; dt&#39;。
中获取数据<强> 更新 强>
如果您想从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();