?
Solved

Change datagridview cell value, use my own date format

Posted on 2006-04-01
10
Medium Priority
?
14,019 Views
Last Modified: 2011-04-14
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!
0
Comment
Question by:vb_jonas
  • 5
  • 4
10 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 16351746
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
 
LVL 14

Expert Comment

by:nayernaguib
ID: 16353041
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
 
LVL 12

Author Comment

by:vb_jonas
ID: 16353259
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Author Comment

by:vb_jonas
ID: 16353278
Changed to

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

still the same parsing error.
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 1000 total points
ID: 16353299
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
 
LVL 12

Author Comment

by:vb_jonas
ID: 16353440
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
 
LVL 12

Author Comment

by:vb_jonas
ID: 16353467
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
 
LVL 12

Author Comment

by:vb_jonas
ID: 16353475
"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
 
LVL 65

Expert Comment

by:rockiroads
ID: 16353948
cool, well done
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16353954
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Introduction to Processes

850 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