title: Writing data in a grid to a text file with formatiing
body: <p>I have a DataGridView in a UI (win form) and now wants to write data in the grid to a text file.</p>
  
  <p>I've tried it with following code. But its not giving the output what I want.</p>
  
  <pre><code>public void WriteToTextFile (DataGridView dgv)
          {
              string file_name = "D:\\test1.txt";
  
          System.IO.StreamWriter objWriter;
  
          objWriter = new System.IO.StreamWriter(file_name);
  
          int count = dgv.Rows.Count;
          for (int row = 0; row &lt; count-1; row++)
          {
              int colCount = dgv.Rows[row].Cells.Count; 
  
              for ( int col = 0; col &lt; colCount-1; col++)  
              {
                  objWriter.WriteLine(dgv.Rows[row].Cells[col].Value.ToString());
              }
          // record seperator could be written here.
          }
          objWriter.Close();
          }
  </code></pre>
  
  <p>Output (only a single item (one grid row) is shown)</p>
  
  <pre><code>8130201489
  
  D.I.S.Peter
  
  292331pslr
  
  0100507563
  
  Sharpe Eyes Pvt Ltd
  
  A002    
  
  SalaryFeb11  
  
  110228
  </code></pre>
  
  <p>But the required out put is... (not this should be a single line )</p>
  
  <pre><code>8130201489D.I.S.PETER     2923319SLR0100507563SHARPEYES PVT LTD A002          SALARYFEB11    110228      @
  </code></pre>
  
  <p><em>How to modify the above code to get this out put?</em><br>
  Also if I need some additional spaces between words how to insert it? After writing I want to change the extension <code>.txt</code> to <code>.dat</code> How to do that. </p>