Background
There is often a need in a project's reporting module to export Grid View records to CSV, So by considering that requirement I decided to write this article especially focusing on beginners and those who want to learn how to export a Grid View to CSV using ASP.NET C#.
Now before creating the application, let us create a table named employee in a database from where we show the records in a Grid View, the table has the following fields (shown in the following image):
Now before creating the application, let us create a table named employee in a database from where we show the records in a Grid View, the table has the following fields (shown in the following image):

I hope you have created the same type of table.
Now create the project as:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).
- Provide the Project name such as ExportGridRecordsToCSV or another as you wish and specify the location.
- Then right-click on Solution Explorer and select "Add New Item" then select Default.aspx page.
- One Button and a grid view.
Now the Default.aspx source code will be as follows:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ExPortGridviewToXML.Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head id="Head1" runat="server">
- <title></title>
- </head>
- <body bgcolor="Silver">
- <form id="form1" runat="server">
- <br />
- <h2 style="color: #808000; font-size: x-large; font-weight: bolder;">
- Article by Vithal Wadje</h2>
- <br />
- <div>
- <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" CellPadding="6"
- ForeColor="#333333" GridLines="None">
- <AlternatingRowStyle BackColor="White" />
- <EditRowStyle BackColor="#7C6F57" />
- <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
- <RowStyle BackColor="#E3EAEB" />
- <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
- <SortedAscendingCellStyle BackColor="#F8FAFA" />
- <SortedAscendingHeaderStyle BackColor="#246B61" />
- <SortedDescendingCellStyle BackColor="#D4DFE1" />
- <SortedDescendingHeaderStyle BackColor="#15524A" />
- <Columns>
- <asp:BoundField DataField="id" HeaderText="id" />
- <asp:BoundField DataField="Name" HeaderText="Name" />
- <asp:BoundField DataField="City" HeaderText="City" />
- <asp:BoundField DataField="Address" HeaderText="Address" />
- <asp:BoundField DataField="Designation" HeaderText="Designation" />
- </Columns>
- </asp:GridView>
- <br />
- <asp:Button ID="Button1" runat="server"
- Text="Export" OnClick="Button1_Click" />
- </div>
- </form>
- </body>
- </html>
Now let us create a function to bind the records to the Grid View from the database. If you are a beginner and don't understand in detail how to bind a Grid View from a database then refer to my following article.
Now, for this article create the following function in the default.aspx.cs page to bind the Grid View:- private void Bindgrid()
- {
- connection();
- query = "select *from Employee";//not recommended this i have written just for example,write stored procedure for security
- com = new SqlCommand(query, con);
- SqlDataAdapter da = new SqlDataAdapter(query, con);
- DataSet ds = new DataSet();
- da.Fill(ds);
- GridView1.DataSource = ds;
- GridView1.DataBind();
- con.Close();
- }
Now, call the preceding function on page load as:
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- Bindgrid();
- }
- }
Now create the following function to export the Grid View records to CSV:



Yogesh KumarPosted May 16, 2018, 6:03 AM
Its not working for me
Vithal WadjePosted Nov 26, 2014, 1:45 AM
thanks
Anil SinghPosted Nov 26, 2014, 12:58 AM
good one
Vithal WadjePosted Nov 14, 2014, 1:30 AM
thanks sir
Jeetendra GundPosted Nov 14, 2014, 12:13 AM
nice article
Vithal WadjePosted Nov 13, 2014, 11:59 PM
thanks
Saineshwar BageriPosted Nov 13, 2014, 11:34 PM
nice article sir