我有一个Gridview,我正在尝试导出到csv文件,但我没有得到任何行只是列名。
<div style ="height:600px; width:auto; overflow:auto;">
<p class="padding">
<asp:GridView ID="GridViewBICARB" runat="server"
AutoGenerateColumns="false"
Caption="Bicarb Operator Checks"
CaptionAlign="Top"
EmptyDataText="Data Not Available"
Width="100%"
AllowPaging="false"
AllowSorting="true"
ShowHeader="true" >
<Columns>
<asp:BoundField DataField="FeedCheck" HeaderText="FeedCheck" />
<asp:BoundField DataField="VisualCheck" HeaderText="VisualCheck" />
</Columns>
<RowStyle CssClass="altrow" />
</asp:GridView>
</p>
</div>
这是我的C#
protected void btnExportReport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=test.csv");
Response.Charset = "";
Response.ContentType = "text/csv";
GridViewBICARB.AllowSorting = false;
GridViewBICARB.AllowPaging = false;
GridViewBICARB.DataBind();
StringBuilder sb = new StringBuilder();
for (int k = 0; k < GridViewBICARB.Columns.Count; k++)
{
//add separator
sb.Append(GridViewBICARB.Columns[k].HeaderText + ',');
}
//append new line
sb.Append("\r\n");
for (int i = 0; i < GridViewBICARB.Rows.Count; i++)
{
for (int k = 0; k < GridViewBICARB.Columns.Count; k++)
{
//add separator
sb.Append(GridViewBICARB.Rows[i].Cells[k].Text + ',');
}
//append new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}
我的输出只是列名。
FeedCheck,VisualCheck,
我已尝试按照其他问题的建议删除以下内容。
GridViewBICARB.AllowPaging = false; GridViewBICARB.DataBind();
但没有帮助......
答案 0 :(得分:0)
我最终将OnPageIndexChanging = "OnPaging"
添加到gridview。单击其中一个寻呼机按钮时,但在GridView控件处理分页操作之前,会引发PageIndexChanging事件。这使您可以提供执行例程的事件处理方法。
就我而言,这是我的常规......
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GridViewBICARB.PageIndex = e.NewPageIndex;
GridViewBICARB.DataBind();
}
NewPageIndex属性用于确定用户在DataGrid控件的页面选择元素中选择的页面的索引。此值通常用于设置DataGrid控件的CurrentPageIndex属性以显示所选页面。
然后我删除了以下内容......
GridViewBICARB.AllowSorting = false;
GridViewBICARB.AllowPaging = false;
GridViewBICARB.DataBind();
而且沃拉有效!