?
Solved

validating Date Textbox in datagridview

Posted on 2009-03-30
10
Medium Priority
?
1,083 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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
 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 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