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

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 stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply