I have been trying to track down an issue in an application that I am writing. I finally deteremined that it isn't some of my code directly creating the issue. I have created a small application to demonstrate this odd behavior.

The odd behavior is that if the DataGridView is loaded with data, then that data is deleted, then another control is selected, then the user goes back and enters data into the DGV, instead of 2 rows (the row with data and the new row), you end up with 4 rows.

To create the example project, create a new project with a form (Form1). Place on the form a DataGridView (DataGridView1) and a Button (Button1). Then, insert the code below into the form.

Steps to reproduce
- Run the application
- Remove the word "Red" from the field and hit Tab (This should remove that row)
- Click on Button1 (There is no code behind Button1)
- Click back into the first cell in the DataGridView
- Type is some text and hit Tab

You should now have 4 rows instead of the expected 2. I have looked all through this and the only thing I can determine is that it has something to do with the DataTable having a deleted row. In no other scenario does this occur.

Any assistance with this would be greatly appreciated! Maybe someone out there has a work around for this.

VB Code:
  1. Public Class Form1
  2.  
  3.     Private Enum ColIndex
  4.         Table_ID = 0
  5.         Color = 1
  6.         SomeField = 2
  7.     End Enum
  8.  
  9.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  10.         Dim dt As New DataTable
  11.  
  12.         With dt
  13.             .Locale = System.Globalization.CultureInfo.InvariantCulture
  14.             .Columns.Add("Table_ID", GetType(Integer))
  15.             .Columns.Add("Color", GetType(String))
  16.             .Columns.Add("SomeField", GetType(String))
  17.             .Rows.Add(.NewRow)
  18.             .Rows(0)(0) = 1
  19.             .Rows(0)(1) = "Red"
  20.         End With
  21.  
  22.         With DataGridView1
  23.             .DataSource = dt
  24.  
  25.             .Columns("Table_ID").Visible = False
  26.             .EditMode = DataGridViewEditMode.EditOnEnter
  27.             .MultiSelect = False
  28.         End With
  29.  
  30.     End Sub
  31.  
  32.     Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
  33.         Dim dgv As DataGridView = DirectCast(sender, DataGridView)
  34.  
  35.         Select Case e.ColumnIndex
  36.             Case ColIndex.Table_ID
  37.             Case ColIndex.Color
  38.                 If dgv.Rows(e.RowIndex).Cells(ColIndex.Color).FormattedValue.ToString.Trim = String.Empty Then
  39.                     ' Delete the row
  40.                     dgv.Rows.Remove(dgv.CurrentRow)
  41.                 End If
  42.             Case ColIndex.SomeField
  43.         End Select
  44.     End Sub
  45. End Class