Export GridView into CSV File : Here we will show how you can generate csv file from the gridview records; data in asp.net using C#.
Incoming search terms
Export GridView into CSV File, Export gridview data into CSV file, Export Gridview data in .csv format, Export GridView Records to CSV Using ASP.Net C#, Gridview export to CSV, how to export gridview data to CSV file, Asp.net export gridview to csv file, export gridview to csv in asp.net C#.
In previous articles we explained Print Image using JavaScript, MDI Parent Window in WPF, Autocomplete Combobox, Transfer Selected Rows from One GridView to Another GridView, Language Translation using Resource File in WPF, 3 Tier Architecture in ASP.NET, Watermark TextBox, Import Excel to SQL Server using SqlBulkCopy, Add Twitter Widget, Tiled Menu Slider in Flash, Generate and Print Barcode, Auto Rotate Content, 3D Spin Menu, Play Sound in Flash, Simple XML Banner Rotator in flash, Load XML Data in Flash, URL Rewrite, Database operation in WPF with Access Database, Get Directions Google Map API and many more. Now we move on export gridview into CSV file.
Following are the steps to export GridView into CSV file.
Over here first we bind the gridview records on page load and set export button to generate the CSV file from the gridview records in Asp.Net using C#.
.ASPX PAGE CODE
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Gridview_To_CSV.aspx.cs" Inherits="Gridview_Export_To_CSV.Gridview_To_CSV" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div style="text-align: center;"> <asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false" Font-Names="Arial" Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green" AllowPaging="true" > <Columns> <asp:BoundField ItemStyle-Width="150px" DataField="Id" HeaderText="Id" /> <asp:BoundField ItemStyle-Width="150px" DataField="FirstName" HeaderText="First Name" /> <asp:BoundField ItemStyle-Width="150px" DataField="LastName" HeaderText="Last Name" /> <asp:BoundField ItemStyle-Width="150px" DataField="Age" HeaderText="Age" /> </Columns> </asp:GridView> </div> <div style="clear: both; height: 25px;"> </div> <div> <asp:Button ID="btnExportToCSV" runat="server" Text="Create CSV File" Width="200px" OnClick="btnExportToCSV_Click"></asp:Button> </div> </form> </body> </html>
ADD CODE-BEHIND SOURCE
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Text; namespace Gridview_Export_To_CSV { public partial class Gridview_To_CSV : System.Web.UI.Page { DataTable dt = new DataTable(); protected void Page_Load(object sender, EventArgs e) { dt.Columns.Add("Id", typeof(int)); dt.Columns.Add("FirstName", typeof(string)); dt.Columns.Add("LastName", typeof(string)); dt.Columns.Add("Age", typeof(int)); dt.Rows.Add(1, "ABCD", "XYZ", 21); dt.Rows.Add(2, "VW", "OPQR", 23); dt.Rows.Add(3, "LMN", "STU", 18); dt.Rows.Add(4, "HIJK", "EFG", 26); gvData.DataSource = dt; gvData.DataBind(); } protected void btnExportToCSV_Click(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv"); Response.Charset = ""; Response.ContentType = "application/text"; gvData.AllowPaging = false; gvData.DataBind(); StringBuilder sb = new StringBuilder(); for (int k = 0; k < gvData.Columns.Count; k++) { //add separator sb.Append(gvData.Columns[k].HeaderText + ','); } //append new line sb.Append("\r\n"); for (int i = 0; i < gvData.Rows.Count; i++) { for (int k = 0; k < gvData.Columns.Count; k++) { //add separator sb.Append(gvData.Rows[i].Cells[k].Text + ','); } //append new line sb.Append("\r\n"); } Response.Output.Write(sb.ToString()); Response.Flush(); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { /* Verifies that the control is rendered */ } } }
RUN PROJECT AND CHECK FINAL OUTPUT

Export GridView into CSV File – Output
Source Code
help@codescratcher.com
The post Export GridView into CSV File appeared first on Code Scratcher.