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: 6
Size: 16.6 KB]()
Here the Code:
Hope you can help me :wave:
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