title: Strange behaviour of DataTable with DataGridView
body: <p>Please explain me what is happening. I have created a WinForms .NET application which has DataGridView on a form and should update database when DataGridView inline editing is used.</p>
  
  <p>Form has <strong>SqlDataAdapter</strong> _da with four SqlCommands bound to it. DataGridView is bound directly to DataTable _names.</p>
  
  <p>Such a CellValueChanged handler:</p>
  
  <pre><code>private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  {
      _da.Update(_names);
  }
  </code></pre>
  
  <p>does not update database state although _names DataTable is updated. All the rows of _names have RowState == DataRowState.Unchanged</p>
  
  <p>Ok, I modified the handler:</p>
  
  <pre><code>private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  {
      DataRow row = _names.Rows[e.RowIndex];
      row.BeginEdit();
      row.EndEdit();
      _da.Update(_names);
  }
  </code></pre>
  
  <p>this variant really writes modified cell to database, but when I attempt to insert new row into grid, I get an error about an absence of row with index e.RowIndex</p>
  
  <p>So, I decided to improve the handler further:</p>
  
  <pre><code>private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  {
      if (_names.Rows.Count&lt;e.RowIndex)
      {
          DataRow row = _names.Rows[e.RowIndex];
          row.BeginEdit();
          row.EndEdit();
      }
      else
      {
          DataRow row = _names.NewRow();
          row["NameText"] = dataGridView1["NameText", e.RowIndex].Value;
          _names.Rows.Add(row);
      }
      _da.Update(_names);
  }
  </code></pre>
  
  <p>Now the really strange things happen when I insert new row to grid: the grid remains what it was until _names.Rows.Add(row); After this line THREE rows are inserted into table - two rows with the same value and one with Null value.</p>
  
  <p>The slightly modified code:</p>
  
  <pre><code>DataRow row = _names.NewRow();
  row["NameText"] = "--------------"
  _names.Rows.Add(row);
  </code></pre>
  
  <p>inserts three rows with three different values: one as entered into the grid, the second with "--------------" value and third - with Null value.</p>
  
  <p>I really got stuck in guessing what is happening.</p>