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.

LVL 12
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

would cellEditing help

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


  Nayer Naguib
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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

vb_jonasAuthor Commented:
Changed to

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

still the same parsing error.
Just a thought, in your DataEror, do u check for exceptions?

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


    End If

What is the format for your DefaultCellstyle? perhaps that is what is causing it. Have a look at this

An alternative could possibly be to get the users to select from the DateTimePicker control instead of them typing.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
                        ' Parse to date
                        If IsDate(e.Value) Then
                            e.Value = CDate(e.Value)
                            ' 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

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.
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? :-)
cool, well done
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.