[2005] DataGridView Odd Behavior
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:
Public Class Form1
Private Enum ColIndex
Table_ID = 0
Color = 1
SomeField = 2
End Enum
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As New DataTable
With dt
.Locale = System.Globalization.CultureInfo.InvariantCulture
.Columns.Add("Table_ID", GetType(Integer))
.Columns.Add("Color", GetType(String))
.Columns.Add("SomeField", GetType(String))
.Rows.Add(.NewRow)
.Rows(0)(0) = 1
.Rows(0)(1) = "Red"
End With
With DataGridView1
.DataSource = dt
.Columns("Table_ID").Visible = False
.EditMode = DataGridViewEditMode.EditOnEnter
.MultiSelect = False
End With
End Sub
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
Dim dgv As DataGridView = DirectCast(sender, DataGridView)
Select Case e.ColumnIndex
Case ColIndex.Table_ID
Case ColIndex.Color
If dgv.Rows(e.RowIndex).Cells(ColIndex.Color).FormattedValue.ToString.Trim = String.Empty Then
' Delete the row
dgv.Rows.Remove(dgv.CurrentRow)
End If
Case ColIndex.SomeField
End Select
End Sub
End Class
Re: [2005] DataGridView Odd Behavior
Anyone have any thoughts on this? I am kinda stuck until I can figure out what is wrong or some sort of work around for it.
Re: [2005] DataGridView Odd Behavior
This doesn't help much, but if it's any consolation, I agree. That is odd behaviour.
Re: [2005] DataGridView Odd Behavior
LOL. As short and straight forward as the code is, it would appear to me to be a bug.
Any thoughts on getting around it?
Re: [2005] DataGridView Odd Behavior
Well without looking too deeply into it, I would suggest the bug (if indeed there is one) is related to the AllowUserToAddRows property. If you do not need many rows to be created, then you could set it to false, and have a button that will add the rows for you (possibly related to an numericupdown control if you want many rows to be added at any one time).
VB Code:
.AllowUserToAddRows = False
This is obviously just a get around. If I get time I will play with the code you posted and see if I can work out what is happening.