Friday, May 17, 2019

DateTime Validation in VB.Net DataGridView Column

Hi, Here I write the code for DateTime input validation of a DataGrid cell in VB.Net. 
Here is the Form Design. Now I code for the input validation of Date Column and Time Column.




















Now my code is written under CellValidating Event.

Private Sub DataGridView1_CellValidating(sender As Object, e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating

        Select Case e.ColumnIndex
            Case 1,2
                Me.DataGridView1.Rows(e.RowIndex).ErrorText = ""
                Dim NewDate As Date
                If DataGridView1.Rows(e.RowIndex).IsNewRow Then Return
                If Not Date.TryParse(e.FormattedValue.ToString(), NewDate) Then
                    e.Cancel = True
                    MsgBox("Enter correct DateTime format", MsgBoxStyle.Critical, "ERROR!")
                    DataGridView1.CurrentCell.Value = ""
                End If
        End Select

    End Sub

If there is any other better option,
Please share it here or mail to me arkaa4@gmail.com

Thank You,
Arka Gupta.

No comments:

Post a Comment