PDA

View Full Version : How to create a text file dynamically in C# windows application


esplstars
07-18-2008, 11:11 AM
Hai ,
I am having a datagrid view with three columns and I have to save the contents of this datagrid view in a text file on a button click.
Note: Here new txt file should be created dynamically on button click and the contents should be saved on that new txt file


Pls help me.
esplstars

timeshifter
07-23-2008, 11:21 PM
Do some research regarding .NET's data controls. If I recall correctly, the DataGridView should have a Rows property which returns a collection of all data rows. Each Row should also contain a property called Cells, which will be just what it sounds like. A simple loop through each cell of each row would be quite easy to do, and building a string based on that is just as easy.

For writing to the text file, look at the System.IO.File namespace. The method to write a text file is literally a single line, and if you want to check to see if a file exists, that's another single line. .NET makes text file manipulation very easy.