C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Export to Text from Gridview


Posted Date: 27 Feb 2007    Resource Type: Code Snippets    Category: ASP.NET GridView

Posted By: kumar       Member Level: Diamond
Rating:     Points: 10



This sample code shows how to export to text file from ASP.NET GridView. This code assumes that you have already bound the data to gridview with ID=Gridview1 and you have allowed paging.

								
protected void btnExportToText_Click(object sender, EventArgs e)
{
StringBuilder str = new StringBuilder();
string strColumnName = string.Empty;

foreach (DataColumn column in dt.Columns)
{
if (strColumnName != "")
{
strColumnName = strColumnName + "\t" + column.ColumnName.ToString();
}
else
{
strColumnName = column.ColumnName.ToString();
}
}

str.Append(strColumnName);
str.Append("\r\n");

foreach (DataRow row in dt.Rows)
{
string strRow = string.Empty;
// Loop through each column.
foreach (DataColumn col in dt.Columns)
{
if (strRow != "")
{
strRow = strRow + "\t" + row[col.ColumnName].ToString();
}
else
{
strRow = row[col.ColumnName].ToString();
}
}
str.Append(strRow);
str.Append("\r\n");
}

Response.Clear();

Response.AddHeader("content-disposition", "attachment;filename=ExportToText.txt");

Response.Charset = "";

Response.ContentType = "application/vnd.text";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

Response.Write(str.ToString());

stringWrite.Dispose();
htmlWrite.Dispose();

Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{
}

and in the design mode

<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeFile="ExportToExcelWortText.aspx.cs" Inherits="ExportToExcelWortText" %>




Responses

Author: Kapil Dhawan    17 Jun 2008Member Level: Gold   Points : 2
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys
Thanks to you
Regards,
Kapil



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Paging and sorting of GridView
Previous Resource: Row number to gridview
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

silicone halloween mask

Contact Us    Privacy Policy    Terms Of Use