Solved

validating Date Textbox in datagridview

Posted on 2009-03-30
10
1,059 Views
Last Modified: 2012-05-06
I am working on a windows application using VB.Net. I have a datagrid view, in which the fourth column is a textbox, in whioch the date is to be entered.I wanted to validate it to make sure that the date entered is in DD/MM/YYYY. How can i do this....

If the entered value is wrong..i wanted to inform the user that the date is not in the correct format and edit it..and the user should not be able to submit the form until he enters the correct date.....
0
Comment
Question by:yuvaratna
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 24025465
You could offer them a date picker control which means they couldn't select an invalid date even if they wanted to
0
 
LVL 29

Expert Comment

by:QPR
ID: 24025476
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24025965
I suggest you to use datepicker instead

Here is how:
http://msdn.microsoft.com/en-us/library/7tas5c80.aspx
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24026783
You can also use CellValueChanged event to validate the date
Example:

    Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
 

        If e.ColumnIndex <> -1 AndAlso e.RowIndex <> -1 Then
 

            ' Column nº4

            If e.ColumnIndex = 3 Then
 

                Dim dt As DateTime

                Dim value As String = Me.DataGridView1(e.ColumnIndex, e.RowIndex).Value.ToString
 

                If value <> String.Empty AndAlso Not DateTime.TryParse(value, dt) Then

                    MessageBox.Show("You must enter a valid date!")

                    Me.DataGridView1(e.ColumnIndex, e.RowIndex).Value = String.Empty

                End If

            End If

        End If
 

    End Sub

Open in new window

0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 24026813
Or you can use CellValidating and force a valid date to leave the cell
    Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
 

        ' Column nº4

        If e.ColumnIndex = 3 Then
 

            Dim dt As DateTime

            If e.FormattedValue.ToString <> String.Empty AndAlso Not DateTime.TryParse(e.FormattedValue.ToString, dt) Then

                MessageBox.Show("You must enter a valid date!")

                e.Cancel = True

            End If
 

        End If
 

    End Sub

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Closing Comment

by:yuvaratna
ID: 31564678
Perfect! thanks a lot
0
 

Author Comment

by:yuvaratna
ID: 24031050
Hi jpaulino can you tell me how can i make sure that the iuser entered something in the textbox ..I mean i wanted to make it required field....the user should not be able to submit the form until he enters some thing in the textbox...
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24031130
The CellValidating event with the e.Cancel = True forces the user to enter something ... is that it ?
0
 

Author Comment

by:yuvaratna
ID: 24031252
but that is allowing the user to submit the form and as it is null, it is throwing a null reference exception...
0
 

Expert Comment

by:McGurk1
ID: 37402541
Be careful using the above code.  It allowed me to enter the following:  12/12/152 as a date.  I don't have the answer yet to how to fully check for the date but wanted to caution anyone trying to use the code as it is above.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now