-
Jul 25th, 2021, 01:35 AM
#1
Thread Starter
New Member
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.
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
-
Jul 25th, 2021, 01:45 AM
#2
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.
-
Jul 25th, 2021, 01:49 AM
#3
Re: Bug in DataGridView ?!
Apart from anything else, this is bad:
vb.net Code:
Private Sub dgvMainProp_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMainProp.CellValueChanged
MyDGV_CellValueChanged(sender, e)
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.
-
Jul 25th, 2021, 04:21 AM
#4
Thread Starter
New Member
Re: Bug in DataGridView ?!
Originally Posted by jmcilhinney
Apart from anything else, this is bad:
vb.net Code:
Private Sub dgvMainProp_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMainProp.CellValueChanged
MyDGV_CellValueChanged(sender, e)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|