Reputation: 6988
This question has been prompted by further discussion on this question.
The following is the routine I'm using for creating the CSV file. Everything works great, except that I only get the columns headers and no rows.
protected void CSVReport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=PeopleReportTest.csv");
Response.Charset = "";
Response.ContentType = "text/csv";
Response.AddHeader("Pragma", "public");
GridView1.AllowSorting = false;
GridView1.AllowPaging = false;
GridView1.DataBind();
StringBuilder sb = new StringBuilder();
for (int k = 1; k < GridView1.Columns.Count; k++)
{
//add separator
sb.Append(GridView1.Columns[k].HeaderText + ',');
}
//append new line
sb.Append("\r\n");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int k = 1; k < GridView1.Columns.Count; k++)
{
//add separator
sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
}
//append new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}
On inspection, I can see that GridView1.Rows[i].Cells[k].Text
always returns an empty value.
So the issue might be with the structure of my gridview. I know that the code wouldn't work if I had TemplateFields
, but I have only one and it's set to invisible.
All other fields are DynamicFields
. Could that be the problem?
Here's the markup:
<asp:GridView ID="GridView1" runat="server"
CssClass="table table-condensed borderless"
HeaderStyle-HorizontalAlign="Left"
AllowPaging="True" AllowSorting="True"
DataSourceID="GridDataSource"
DataKeyNames="PersonID" OnRowCommand="GridView1_RowCommand"
AutoGenerateColumns="false"
OnDataBound="GridView1_DataBound" PageSize="15">
<Columns>
<asp:templatefield itemstyle-width="0%" visible="false">
<itemtemplate>
<aspf:dynamichyperlink id="detailshyperlink" runat="server"
text="details" />
</itemtemplate>
</asp:templatefield>
<asp:DynamicField DataField="Name" HeaderText="Name"/>
<asp:DynamicField DataField="PersonCategory" HeaderText="Category" />
<asp:DynamicField DataField="PositionDescription" HeaderText="Position" />
<asp:DynamicField DataField="Description" HeaderText="Topic" />
<asp:DynamicField DataField="FellowsResponsibility" HeaderText="Responsibility" />
<asp:DynamicField DataField="PartnerSurname" HeaderText="Partner"/>
</Columns>
<PagerStyle CssClass="footer" />
<PagerTemplate>
<asp:GridViewPager runat="server" />
</PagerTemplate>
<EmptyDataTemplate>
There are currently no items in this table.
</EmptyDataTemplate>
</asp:GridView>
Upvotes: 1
Views: 1467
Reputation: 6988
OK I've found what the problem was.
I couldn't find other similar scenarios around, so I'm answering my own question in case someone will find it helpful.
The solution was simply to replace all my DynamicFields
with standard BoundFields
and BOOM all the data was there.
Next time I won't be using DynamicData
framework, that's for sure.
Upvotes: 1