?
Solved

validating Date Textbox in datagridview

Posted on 2009-03-30
10
Medium Priority
?
1,085 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
Industry Leaders: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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