I have a datagridvied with several columns and one with date and the format is

Me.DataGridView1.Columns("Doopdatum").DefaultCellStyle.Format = "yyyy - MM - dd dddd"

or

Me.DataGridView1.Columns("Doopdatum").DefaultCellStyle.Format = "dddd dd - MM - yyyy"

Depends on the button i press

My problem is when i reverse from "yyyy - MM - dd dddd" to "dddd dd - MM - yyyy" It dos not work proper

I have the folowing code to reverse the date

Private Sub DoopdatumOmdraaienToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DoopdatumOmdraaienToolStripMenuItem.Click
        If DoopdatumOmdraaienToolStripMenuItem.CheckState = CheckState.Checked Then
            Me.DataGridView1.Columns("Doopdatum").DefaultCellStyle.Format = "dddd  dd - MM - yyyy"
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim date1 As Date = row.Cells(5).Value
                row.Cells(5).Value = Format(date1.ToString("dddd  dd - MM - yyyy"))
            Next

            With My.Settings
                .Setting25 = True
                My.Settings.Save()
            End With
        End If

        If DoopdatumOmdraaienToolStripMenuItem.CheckState = CheckState.Unchecked Then
            Me.DataGridView1.Columns("Doopdatum").DefaultCellStyle.Format = "yyyy - MM - dd  dddd"
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim date1 As Date = row.Cells(5).Value
                row.Cells(5).Value = Format(date1.ToString("yyyy - MM - dd  dddd"))
            Next
            With My.Settings
                .Setting25 = False
                My.Settings.Save()
            End With

        End If
    End Sub

My outut gives the following when it is in dddd dd MM yyyy.
1111 - 01 - 31 Dinsdag
1111 - 01 - 04 Zaterdag
Donderdag 05 - 01 - 1111

and so on

What am i doing wrong
I tried everything Please help

Recommended Answers

All 5 Replies

I see what happens but i do not know why.
When i order the date column in the datagridview to asc or desc the Id column is
Not in order then it is like 4 3 7 1 2 6 5 or what else Or what else. Depence on the date.
When i reverse the date of each row than it is not working proper.
Wen i orde the id column to asc or desc so that the id column is 1 2 3 4 5 6 7
The i can reverse the date column proper

I have created a attechment you can download it at
www.dohmen-stamboom.nl/downloads/DATE_REVERSE.zip

its created with VS 2015 Net framework 4.6.1 dutch version
Database is SQLITE

Reorder the dates and reverse it Code is all included

Did you try 'jjjj' instead of 'yyyy' ?

Yes jjjj dos not work. Gives an error
The conversion of string Tuesday 03 - 01 - jjjj to Type Date is invalid.

The problem is that you enter a string into a cell formatted for a date.
Just change
row.Cells(5).Value = Format(date1.ToString("yyyy - MM - dd dddd"))
into
row.Cells(5).Value = date1
that should work.
Me.DataGridView1.Columns("Doopdatum").DefaultCellStyle.Format = "yyyy - MM - dd dddd"
defines the display of the value, but the value is always a date.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.