What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 7626 |  Welcome, Guest!   Register  Login
Home > Articles > Windows Forms > Exporting Data to txt file using C#

Exporting Data to txt file using C#

Article posted by Syedshakeer on 5/30/2009 | Views: 29108 | Category: Windows Forms | Level: Intermediate red flag


In this Article i am going to explain you how to Export a DataGridView(Windows) data to a TextFile(.txt)

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' varchar,'salary' number and i Inserted Some Data in 'emp' Table.
Next Retrive the Data in DataGridview(windows).

AfterRetreving OutPut of Datagridview as below :


What you have data, present  in datagrideview that data should be export in to the txtfile.

creating 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" + 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

 

After Exporting Open your txt file and you can see the output :

This is the simple way Exporting Data to a textfile.Plz Give me Feed back about this Article.


 

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:2 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, February 05, 2009
Level:Starter
Status: [Member]
Biography:Shakeer Hussain has completed his Master of Computer Applications degree from Deccan College of engg and technology of Osmania University.He is a MVM of www.dotnetspider.com.He has good experience in the areas of ASP.NET, C#.NET, VB.NET, SQL SERVER 2000/2005 and Windows Mobile. He has worked in Windows Mobile,Web Applicatin and ERP projects.
>> Write Response - Respond to this post and get points
Related Posts

In Windows application development, we have few built-in extender components like ErrorProvider, Tooltip, etc. Extender component will add few more properties to built-in controls to enhance the functionality. In this article we will discuss about creating a custom extender component for Windows controls.

This article describes sample usage of MDI form.

To Call C# function from VB.Net.

In this article you will know how to display records in a textboxes as First,Next,Previous and Last records

In this article explains you how to create Stopwatch Application using C#.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/26/2013 2:06:03 AM