0

I've been trying to export my data from my DataGridView into a text file, it only works when I already have a txt doc, but I want it to auto generate new ones after every button click. Help would be appreciated. This is similar to what I have now:

TextWriter writer = new StreamWriter(@"C:\folder\Text.txt");
for(int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
     for(int j = 0; j < dataGridView1.Columns.Count; j++)
     {                        
          writer.Write("\t"+dataGridView1.Rows[i].Cells[j].Value.ToString()+"\t"+"|");
     }
     writer.WriteLine("");
     writer.WriteLine("-----------------------------------------------------");
}
 writer.Close();
 MessageBox.Show("Data Exported");

It works now, thanks for the help. Anyone interested here's the code:

string datetime = DateTime.Now.ToString("yyyy-MMMM-dddd-HH-mm-ss");
        string filePath = $@"C:\folder\{datetime}.txt";

        TextWriter writer = new StreamWriter(filePath);
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            for (int j = 0; j < dataGridView1.Columns.Count; j++)
            {
                writer.Write("\t" + dataGridView1.Rows[i].Cells[j].Value.ToString() + "\t" + "|");
            }
            writer.WriteLine("");
            writer.WriteLine("-----------------------------------------------------");
        }
        writer.Close();
        //MessageBox.Show("Data Exported");
  • Possible duplicate of [Creating text file in C#](https://stackoverflow.com/questions/27823789/creating-text-file-in-c-sharp) – Cizzl Mar 21 '19 at 08:36

3 Answers3

0

You can create before this code your own text file like this:

string path = "C:\folder\Text.txt";
if (!File.Exists(path))
{
    File.Create(path);
}

with file exist you can check if the file already exists, if not then create it.

Cizzl
  • 324
  • 2
  • 11
0

Try to use File.WriteAllText

string path = "C:\folder\Text"
for(int j = 0; j < dataGridView1.Columns.Count; j++)
{
File.WriteAllText(path + j + ".txt", yourTextToWrite);
}

It will generate new file if .txt file is not existing.

To prepare yourTextToWrite you can use StringBuilder class:

StringBuilder yourTextToWrite = new StringBuilder();
yourTextToWrite.Append("text1\n");
yourTextToWrite.Append("text2\n");  
0

You just need to make some kind of unique name for your file. So do something like the below, where you use something (it's up to you what) like a DateTime string.

string datetime = DateTime.Now.ToString("yyyy-MM-dd-HHMM");
string filePath = $@"C:\folder\Text{datetime}.txt";
File.Create(filePath);
TextWriter writer = new StreamWriter(path);
for(int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
     for(int j = 0; j < dataGridView1.Columns.Count; j++)
     {                        
          writer.Write("\t"+dataGridView1.Rows[i].Cells[j].Value.ToString()+"\t"+"|");
     }
     writer.WriteLine("");
     writer.WriteLine("-----------------------------------------------------");
}
 writer.Close();
 MessageBox.Show("Data Exported");

Have a look at the documentation around File. There are lots of examples on how one should work with files.

Topher
  • 1,011
  • 11
  • 19
  • In the 4th line of text, did you mean new StreamWriter(path) or new StreamWriter(filePath). When i used i got the error that the textfile is being used by another process. –  Mar 21 '19 at 08:39
  • Sorry, I didn't test it, but was trying to get the concept across more than write the exact code you needed. I take it it helped though? – Topher Mar 21 '19 at 10:41
  • Yes i just had to edit some lines but other than that it worked, thanks allot mate. –  Mar 21 '19 at 10:43