|
-
Jun 21st, 2006, 12:55 PM
#1
Thread Starter
Fanatic Member
[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
My.Settings.Signature = String.Empty
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|