Results 1 to 4 of 4

Thread: Bug in DataGridView ?!

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    2

    Exclamation Bug in DataGridView ?!

    Dear VB.Net - Pros,

    I don't know why, but the third row of my DataGridView is behaving strangely. And I can only enter one char. Although they are all controlled by the same events.
    Maybe someone of you sees something.

    Name:  25-07-_2021_08-18-56.jpg
Views: 214
Size:  16.6 KB

    Here the Code:
    Code:
        Private Sub dgvMainProp_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMainProp.CellClick
            If (e.ColumnIndex > 0) And e.RowIndex <> -1 Then
                With DirectCast(sender, DataGridView)
                    If TypeOf dgvMainProp(e.ColumnIndex, e.RowIndex) Is DataGridViewComboBoxCell Then
                        .CurrentCell = .Rows(e.RowIndex).Cells(e.ColumnIndex)
                        .BeginEdit(True)
                        DirectCast(.EditingControl, System.Windows.Forms.DataGridViewComboBoxEditingControl).DroppedDown = True
                    End If
                End With
            End If
    
        End Sub
    
        Private Sub dgvMainProp_DefaultValuesNeeded(sender As Object, e As DataGridViewRowEventArgs) Handles dgvMainProp.DefaultValuesNeeded
            With e.Row
                .Cells(1).Value = OptionText(0)
    
            End With
        End Sub
    
        Private Sub dgvMainProp_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMainProp.CellValueChanged
    
            MyDGV_CellValueChanged(sender, e)
    
        End Sub
    
    
        Private Sub dgvMainProp_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles dgvMainProp.CurrentCellDirtyStateChanged
            If dgvMainProp.IsCurrentCellDirty Then
                dgvMainProp.CommitEdit(DataGridViewDataErrorContexts.Commit)
            End If
        End Sub
    
        Private Function IntOnly(MyValue As String) As Boolean
            Return System.Text.RegularExpressions.Regex.IsMatch(MyValue.Trim, "[^0-9]+")
        End Function
    
        Private Sub MyDGV_CellValueChanged(MyGDV As DataGridView, e As DataGridViewCellEventArgs)
            'Wenn ein Eigenschaftstyp gewählt ist ...
            If MyGDV.Rows(e.RowIndex).Cells(1) IsNot DBNull.Value Then
    
                '**************...und wenn "ja oder nein" als Type gewählt wurde...********************************
                If CStr(MyGDV.Rows(e.RowIndex).Cells(1).Value) = OptionText(3) Then
    
                    '...dann für den Wert eine Combobox mit "Ja" und "Nein" zur Verfügung stellen
                    Dim cbYesNo As New DataGridViewComboBoxCell
                    cbYesNo.Items.Add(OptionText(4))
                    cbYesNo.Items.Add(OptionText(5))
                    MyGDV.Rows(e.RowIndex).Cells(2) = cbYesNo
    
                    With MyGDV.Rows(e.RowIndex)
                        If .Cells(1).Value = OptionText(3) And IsDBNull(.Cells(2).Value) Then
                            .Cells(2).Value = OptionText(4) '"Yes"
                        ElseIf .Cells(1).Value = OptionText(3) And CStr(.Cells(2).Value) = OptionText(5) Then 'No
                            .Cells(2).Value = OptionText(5) '"No"
                        ElseIf .Cells(1).Value = OptionText(3) And CStr(.Cells(2).Value) = OptionText(4) Then 'Yes
                            .Cells(2).Value = OptionText(4) '"Yes"
                        ElseIf .Cells(1).Value = OptionText(3) And CStr(.Cells(2).Value) <> OptionText(4) And CStr(.Cells(2).Value) <> OptionText(5) Then 'Yes , No
                            .Cells(2).Value = OptionText(4) '"Yes"
                        End If
                    End With
                End If
    
    
                '**************...und wenn "TEXT" als Type gewählt wurde...********************************
                If CStr(MyGDV.Rows(e.RowIndex).Cells(1).Value) = OptionText(0) Then
                    Dim txtEmpty As New DataGridViewTextBoxCell
                    MyGDV.Rows(e.RowIndex).Cells(2) = txtEmpty
                End If
    
    
            End If
            MyGDV.Invalidate()
        End Sub
    
        Private Sub dgvMainProp_CellLeave(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMainProp.CellLeave
            '**************...und wenn ANZAHL als Type gewählt wurde...********************************
    
            If CStr(dgvMainProp.Rows(e.RowIndex).Cells(1).Value) = OptionText(2) Then
    
                If dgvMainProp.Rows(e.RowIndex).Cells(2) IsNot DBNull.Value Then
                    With dgvMainProp.Rows(e.RowIndex)
    
                        If Trim(.Cells(2).Value.ToString) <> "" Then
    
                            If IntOnly(.Cells(2).Value.ToString) Then
                                MessageBox.Show("Ungültiger Wert für Typ""Anzahl""eingegeben", "SWXHelper", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                .Cells(2).Value = DBNull.Value
    
                            End If
                        End If
                    End With
                End If
            End If
    
            '**************...und wenn DATUM als Type gewählt wurde...********************************
            If CStr(dgvMainProp.Rows(e.RowIndex).Cells(1).Value) = OptionText(1) Then
    
                If dgvMainProp.Rows(e.RowIndex).Cells(2) IsNot DBNull.Value Then
                    With dgvMainProp.Rows(e.RowIndex)
    
                        If Trim(.Cells(2).Value.ToString) <> "" Then
    
    
    
                            ' several possible format styles
                            Dim formats() As String = {"d-MM-yyyy", "dd-MM-yyyy", "dd-M-yyyy", "d-M-yyyy", "d.MM.yyyy", "dd.MM.yyyy", "dd.M.yyyy", "d.M.yyyy", "d/MM/yyyy", "dd/MM/yyyy", "dd/M/yyyy", "d/M/yyyy"}
    
                            Dim thisDt As DateTime
    
                            Dim MyDate As String = .Cells(2).Value.ToString
                            ' this should work with all 3 strings above
                            If DateTime.TryParseExact(MyDate, formats, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, thisDt) Then
                                Console.WriteLine("Success! {0}", thisDt.ToString)
                                'MsgBox("Datum")
                                Dim Newformat As String = "dd.MM.yyyy"
                                .Cells(2).Value = thisDt.ToString(Newformat)
                            Else
                                MessageBox.Show("Ungültiger Wert für Typ""Datum""eingegeben", "SWXHelper", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                .Cells(2).Value = DBNull.Value
                            End If
    
    
    
                        End If
    
    
                    End With
                End If
            End If
    
        End Sub
    Hope you can help me

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Bug in DataGridView ?!

    Debug your code, which you should have done before posting. Place a breakpoint at the top of each event handler and then, when execution breaks, step through the code line by line. You can examine the state of all your variables, etc, before and after each step and compare what actually happens to your expectations. When the two don't match, you have found an issue. Once you know what the actual issue is, you can attempt to deal with it. If you're unable to deal with it, you can post here and provide us with all the relevant information and no irrelevant information, rather than our having to wade through the code in order to even find the issue before trying to diagnose it.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Bug in DataGridView ?!

    Apart from anything else, this is bad:
    vb.net Code:
    1. Private Sub dgvMainProp_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMainProp.CellValueChanged
    2.  
    3.     MyDGV_CellValueChanged(sender, e)
    4.  
    5. End Sub
    You should never be calling event handlers directly. The values you are passing are not the same as would be generated by the control when it raises its own event. I wouldn't be surprised if that's the root of the issue.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    2

    Re: Bug in DataGridView ?!

    Quote Originally Posted by jmcilhinney View Post
    Apart from anything else, this is bad:
    vb.net Code:
    1. Private Sub dgvMainProp_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMainProp.CellValueChanged
    2.  
    3.     MyDGV_CellValueChanged(sender, e)
    4.  
    5. End Sub
    You should never be calling event handlers directly. The values you are passing are not the same as would be generated by the control when it raises its own event. I wouldn't be surprised if that's the root of the issue.

    MyDGV_CellValueChanged is just a procedure. There is no DataGridView with this name.
    If I write the code of MyDGV_CellValueChanged directly into the CellValueChanged event of my DataGridView I get the same error.
    Thanks for your thought

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