title: Datagridview Windows Form
body: <p>This is my file (test.csv) which was saved by datagridview, the code below reads the file and paste all the values into datagridview.           </p>
  
  <pre><code>InitializeComponent();
  try
  {
     //gbSeries.Enabled = false;
     //AssignValues();
     //btnSave.Enabled = true;
  
     // read and insert a new line
     System.IO.StreamReader csv_file = File.OpenText("c:/test.csv");
  
     while (csv_file.Peek() &gt;= 0)
     {
        // read and add a line
        string line = csv_file.ReadLine();
        string[] vals = line.Split(';');
  
        dataGridView1.Rows.Add(new object[] 
        { 
           vals[0],
           vals[1],
           vals[2],
           vals[3],
           vals[4],
           vals[5],
           vals[6],
           vals[7]
           //cansim,
           //freeze });
        });
     }
     csv_file.Close();
  }
  </code></pre>
  
  <p>I save my datagrid view data like so:</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
  {
      try
      {
          System.IO.StreamWriter csvFileWriter = new StreamWriter(CsvFpath, false);
          string columnHeaderText = "";
          int countColumn = dataGridView1.ColumnCount - 1;
  
          if (countColumn &gt;= 0)
          {
              columnHeaderText = dataGridView1.Columns[0].HeaderText;
          }
  
          for (int i = 1; i &lt;= countColumn; i++)
          {
              columnHeaderText = columnHeaderText + ';' + dataGridView1.Columns[i].HeaderText;
          }
  
          csvFileWriter.WriteLine(columnHeaderText);
          foreach (DataGridViewRow dataRowObject in dataGridView1.Rows)
              dataGridView1.Rows = 1;
          {
              if (!dataRowObject.IsNewRow)
              {
                  string dataFromGrid = "";
                  dataFromGrid = dataRowObject.Cells[0].Value.ToString();
  
                  for (int i = 1; i &lt;= countColumn; i++)
                  {
                      dataFromGrid = dataFromGrid + ';' + dataRowObject.Cells[i].Value.ToString();
                  }
                  csvFileWriter.WriteLine(dataFromGrid);
              }
          }
          csvFileWriter.Flush();
          csvFileWriter.Close();
      }
      catch (Exception exceptionObject)
      {
          MessageBox.Show(exceptionObject.ToString()); // If I don't have a value in the column then it throws exception. I don't want that. 
      }
  }
  </code></pre>
  
  <p>The first row which contains names of columns, I just want to save it once for all time. The problem is, every single time when i edit my datagridview and save it. The first row which contains names of colums (as said before) comes twice and if I edit again and again it gets rownames + 1 bigger everytime. </p>
  
  <pre><code>first time:
  Row names ---------
  data --------------
  
  After editing:
  
  Row names ---------
  Row names ---------
  data --------------
  
  After editing:
  
  Row names ---------
  Row names ---------
  Row names ---------
  data --------------
  </code></pre>
  
  <hr>
  
  <p>All my datagridview columns need to have a value, before I can save my data. But I want to save data with empty columns also, is there anything I can do?? When there is a empty column it throws an exception and then all my data is GONE!</p>
  
  <pre><code>MessageBox.Show(exceptionObject.ToString()); // If I don't have a value in the column then it throws exception. I don't want that.
  </code></pre>
  
  <p>How should I save the first line/row once and not everytime. And when I read file back into datagridview, I don't want to read the first row which contains names ( which is already there). </p>