Tag Archives: CSV

Export Gridview Data to CSV File in ASP.NET

In this post, I will explain how to export Gridview data to Comma-separated values (CSV) file in ASP.NET

Coding Part:

1. Let’s add a Gridview control to aspx page and fill it with our sample Employees table from Northwind Database. Here is a code snippet.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True">
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" ReadOnly="True" InsertVisible="False" SortExpression="EmployeeID"></asp:BoundField>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName"></asp:BoundField>
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName"></asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address"></asp:BoundField>
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City"></asp:BoundField>
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode"></asp:BoundField>
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country"></asp:BoundField>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:NorthwindConnectionString %>' SelectCommand="SELECT [EmployeeID], [FirstName], [LastName], [Address], [City], [PostalCode], [Country] FROM [Employees]"></asp:SqlDataSource>

2. Set the connection string in the web.config file as shown below.

<add name="NorthwindConnectionString" connectionString="Data Source=localhost;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123456"
providerName="System.Data.SqlClient" />

3. Now let’s write a code to export this Gridview data to CSV.

4. Let’s add a asp button to aspx page, call it as “Export to CSV” and on the “onClick” event of the button write following code.

Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewtoCSVExport.csv");
Response.Charset = string.Empty;
Response.ContentType = "application/text";

GridView1.AllowPaging = false;

StringBuilder stringBuilder = new StringBuilder();
for (int index = 0; index < GridView1.Columns.Count; index++)
//add separator
stringBuilder.Append(GridView1.Columns[index].HeaderText + ',');
//append new line
for (int index = 0; index < GridView1.Rows.Count; index++)
for (int index2 = 0; index2 < GridView1.Columns.Count; index2++)
//add separator
stringBuilder.Append(GridView1.Rows[index].Cells[index2].Text + ',');
//append new line


To avoid the exception like “Control of type must be placed inside a form tag with runat=server.“, we need to add following method in the code behind.

public override void VerifyRenderingInServerForm(Control control)
/* Verifies that the control is rendered */

5. Once done, rebuild the solution and run it. You will see following output on the page.

6. Now let’s click on “Export to CSV” button to see the output in notepad.

Source Code Download

Github [Repository link]

Box.com [Direct Link to Zip file]