Solved

validating Date Textbox in datagridview

Posted on 2009-03-30
10
1,069 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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