Change datagridview cell value, use my own date format

I have a datagridview with a column populated from a datetime field. I would like to give the user the option to enter dates in the format "051231" for 2005-12-31. The dateparser doesnt understand that format string as a date, therfore I need to do some parsing first. However I cannot find the right event to use for this. Seems that what ever I do the DataError event fires (and I cannot figure out how to go from there to give the cell a proper date, not throwing an error.) The dataset behind is strongly typed.

Thanx!
LVL 12
vb_jonasAsked:
Who is Participating?
 
rockiroadsCommented:
Just a thought, in your DataEror, do u check for exceptions?

    If Not e.Exception Is Nothing AndAlso _
        e.Context = DataGridViewDataErrorContexts.Commit Then

        MessageBox.Show("Error.")

    End If


What is the format for your DefaultCellstyle? perhaps that is what is causing it. Have a look at this  http://msdn2.microsoft.com/en-us/library/hezscd0d(VS.80).aspx





An alternative could possibly be to get the users to select from the DateTimePicker control instead of them typing.
0
 
rockiroadsCommented:
would cellEditing help

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.cellbeginedit(VS.80).aspx

on the cellEndEdit., u could perhaps put your code there
0
 
nayernaguibCommented:
CellEndEdit event will fire *after* data validation. You need to put your code in the event handler of the CellValidating event instead.

_______________

  Nayer Naguib
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
vb_jonasAuthor Commented:
I have tried theese, and I can validate and cancel the edit, but I cannot figure out how to change the given value.

        Dim sInstring As String
        If e.ColumnIndex = 0 Then
            sInstring = DataGridView1.CurrentCell.Value
            If Not IsDate(sInstring) Then
                If sInstring.Length = 6 Then
                    sInstring = sInstring.Insert(4, "-").Insert(2, "-")
                    DataGridView1.CurrentCell.Value = sInstring
                End If
            End If
        End If


Still fires DataError (even though the new string is an allowed datastring, ie 05-12-31). And if I cancel the error in the DataError event the cell value goes back to the default value.

"String was not recognized as a valid datetime..." "...please handle the DataError event." But I cannot (?) correct the data there either (just show error).

Isnt there a event that activates before the system parses for date? Or am I not correct when I change the value with DataGridView1.CurrentCell.Value = sInstring (I have also tried to CDate the sInstring there). When I watch the CurrentCell.Value I see it changes from 051231 to 05-12-31, but the datetime-parser must have taken the value somewhrere else.
0
 
vb_jonasAuthor Commented:
Changed to

DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = sInstring

still the same parsing error.
0
 
vb_jonasAuthor Commented:
I followed your link and from there to CellParsing and solved the problem this way:

    Private Sub DataGridView1_CellParsing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellParsingEventArgs) Handles DataGridView1.CellParsing
        If Me.DataGridView1.Columns(e.ColumnIndex).Name = "DateColumn" Then
            If e IsNot Nothing Then
                If e.Value IsNot Nothing Then
                    Try
                        ' Parse to date
                        If IsDate(e.Value) Then
                            e.Value = CDate(e.Value)
                        Else
                            ' if the date is in the format 051231:
                            If e.Value.ToString.Length = 6 And IsNumeric(e.Value) Then
                                e.Value = CDate(e.Value.ToString().Insert(4, "-").Insert(2, "-"))
                            End If
                        End If
                        e.ParsingApplied = True

                    Catch ex As FormatException
                        ' Set to false in case another CellParsing handler
                        ' wants to try to parse this DataGridViewCellParsingEventArgs instance.
                        e.ParsingApplied = False
                    End Try
                End If
            End If
        End If

    End Sub


thanx!
0
 
vb_jonasAuthor Commented:
DataGridView.CellParsing Event  
Note: This event is new in the .NET Framework version 2.0.

Occurs when a cell leaves edit mode if the cell value has been modified.
0
 
vb_jonasAuthor Commented:
"An alternative could possibly be to get the users to select from the DateTimePicker control instead of them typing."

Well - yes, but we dont like compromises do we? :-)
0
 
rockiroadsCommented:
cool, well done
0
 
rockiroadsCommented:
I developed an app sometime ago, which used the datetime picker control (v6 visual studi). Some users prefer this. Can give them an option (in their personal profile), whether to automatically display or not.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.