Solved

validating Date Textbox in datagridview

Posted on 2009-03-30
10
1,079 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 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
 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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