Results 1 to 5 of 5

Thread: [2005] DataGridView Odd Behavior

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    [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:
    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
    My.Settings.Signature = String.Empty

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    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.
    My.Settings.Signature = String.Empty

  3. #3
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394

    Re: [2005] DataGridView Odd Behavior

    This doesn't help much, but if it's any consolation, I agree. That is odd behaviour.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    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?
    My.Settings.Signature = String.Empty

  5. #5
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394

    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:
    1. .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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width