C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » Visual Studio »

Export Datagridview(Windows) data to a txtfile(.txt)


Posted Date: 25 May 2009    Resource Type: Articles    Category: Visual Studio
Author: Syed Shakeer Hussain Member Level: Diamond    
Rating: 1 out of 5Points: 10



Hi,
In this Article i am going to explain you how to Export a DataGridView(Windows) data to a TextFile(.txt)
first i created a Table with a name 'emp'.it has 3 columns as 'id' number,'empname' number,'salary' number and i Inserted Some Data in 'emp' Table.
Next Retrive the Data in DataGridview(windows).
What you have data in datagideview that should be export to the txtfile.

How to create a text file with a coding.
Below line is creating a textfile with a name file11.just you have to give a path where your text file should be located.here I am saving my file in ‘F Drive’.

FileStream fileStream = new FileStream(@"F:\file11.txt", FileMode.Create);

Here FileMode.Create will automatically create a textfile with a name called file11.txt in ‘F drive’ with a FileStream Class.
If your are using Io Streams class then you have to Import a NameSpace as Using Sytstem.Io;
Write below code in one Button

TextWriter sw = new StreamWriter(@"F:\\file11.txt");
int rowcount = dataGridView1.Rows.Count;
for (int i = 0; i < rowcount - 1; i++)
{
sw.WriteLine(dataGridView1.Rows[i].Cells[0].Value.ToString() + "\t" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "\t\t" + dataGridView1.Rows[i].Cells[2].Value.ToString());
}
sw.Close();//Don’t Forget Close the TextWriter Object(sw)
MessageBox.Show("Data Successfully Exported");

In above code Cells[0] is first column in datagridview
Cells[1] is second column in datagridview

Cells[2] is third column in datagridview



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Export Datagridview(Windows) data to a txtfile(.txt)  .  

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: Keyword and their purpose in C#
Previous Resource: Shortcut Keys of Edit Menu in VS 2008
Return to Discussion Resource Index
Post New Resource
Category: Visual Studio


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use