title: Writing to a text file from DataGridView in C# windows form
body: <p>I want to write the data from my <code>DataGridView</code> to a textfile. </p>
  
  <p>The following code writes the data but it is not formatted correctly.</p>
  
  <pre><code>StreamWriter sW = new StreamWriter(dirLocationString);
  
  string lines = "";
  
  for (int row = 0; row&lt; numRows1; row++){
       for (int col = 0; col &lt; 4; col++)
           {
  
            lines = lines + " " + dataGridView1.Rows[row].Cells[col].Value.ToString();
  
           }
  
        sW.WriteLine(lines);
  }
  
     sW.Close();   
  </code></pre>
  
  <p>I want the output format to be like:</p>
  
  <pre><code>AAAA, BBBB, CCCC, DDDD 
  </code></pre>
  
  <p>But instead it displays the following:</p>
  
  <pre><code>AAAA BBBB CCCC DDDD
  AAAA BBBB CCCC DDDD AAAA BBBB CCCC DDDD
  AAAA BBBB CCCC DDDD AAAA BBBB CCCC DDDD AAAA BBBB CCCC DDDD
  and so on..
  </code></pre>