Gridview empty rows when exporting to CSV

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.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;

        StringBuilder sb = new StringBuilder();
        for (int k = 1; k < GridView1.Columns.Count; k++)
            //add separator
            sb.Append(GridView1.Columns[k].HeaderText + ',');
        //append new line
        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

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"
            AllowPaging="True" AllowSorting="True"
            DataKeyNames="PersonID" OnRowCommand="GridView1_RowCommand"
            OnDataBound="GridView1_DataBound" PageSize="15">
                <asp:templatefield itemstyle-width="0%" visible="false">
                        <aspf:dynamichyperlink id="detailshyperlink" runat="server"
                            text="details" />


                <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"/>

            <PagerStyle CssClass="footer" />
                <asp:GridViewPager runat="server" />
                There are currently no items in this table.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply