Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Sorting date column in datagridview

Sorting date column in datagridview

Scheduled Pinned Locked Moved Visual Basic
tutorialcsharpalgorithmshelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    helelark123
    wrote on last edited by
    #1

    Hello, I am programming in vb.net (VS2005). I am trying to sort date column in datagridview but with no success. When I am clicking on header of the column to sort, I got sorting only by day: for example: 01/05/2009 02/03/2009 03/08/2008 ... instead of getting: 03/08/2008 02/03/2009 01/05/2009 I tried to parse the column to DateTime but it dosn't help. I tried in Cell_Parsing event also to Parse but the evnet didn't fire at all. Can you please give any idea how to do it. Thank you for help

    Shay Noy

    D 1 Reply Last reply
    0
    • H helelark123

      Hello, I am programming in vb.net (VS2005). I am trying to sort date column in datagridview but with no success. When I am clicking on header of the column to sort, I got sorting only by day: for example: 01/05/2009 02/03/2009 03/08/2008 ... instead of getting: 03/08/2008 02/03/2009 01/05/2009 I tried to parse the column to DateTime but it dosn't help. I tried in Cell_Parsing event also to Parse but the evnet didn't fire at all. Can you please give any idea how to do it. Thank you for help

      Shay Noy

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      Try setting the DataGridViewColumn.DefaultCellStyle.Format as "d". Is the datagridview bound?

      It's not necessary to be so stupid, either, but people manage it. - Christian Graus

      H 1 Reply Last reply
      0
      • D dan sh

        Try setting the DataGridViewColumn.DefaultCellStyle.Format as "d". Is the datagridview bound?

        It's not necessary to be so stupid, either, but people manage it. - Christian Graus

        H Offline
        H Offline
        helelark123
        wrote on last edited by
        #3

        It doesn't help too. The datagridview is Unbound. I had added the columns in design mode through the "Edit Columns" feature of the datagridview. The sortMode is set to Automatic and as you said the DataGridViewColumn.DefaultCellStyle.Format is set to d. Thank you

        Shay Noy

        D 1 Reply Last reply
        0
        • H helelark123

          It doesn't help too. The datagridview is Unbound. I had added the columns in design mode through the "Edit Columns" feature of the datagridview. The sortMode is set to Automatic and as you said the DataGridViewColumn.DefaultCellStyle.Format is set to d. Thank you

          Shay Noy

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          You will need to handle the SortCompare event for the datagridview and sort the data there.

          It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

          H 1 Reply Last reply
          0
          • D dan sh

            You will need to handle the SortCompare event for the datagridview and sort the data there.

            It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

            H Offline
            H Offline
            helelark123
            wrote on last edited by
            #5

            Thank you, I will try this.

            Shay Noy

            P 1 Reply Last reply
            0
            • H helelark123

              Thank you, I will try this.

              Shay Noy

              P Offline
              P Offline
              pekatete
              wrote on last edited by
              #6

              Hi - if you are still searching for an answer, I got one for you. It involves implementing the icomparer class. Add this to the your form:

              Public Class DataGridViewSortRowsByDateTime
                  Implements System.Collections.IComparer
                  Private Direction As Integer = 1
                  Public Sub New(ByVal so As SortOrder)
                      If so = SortOrder.Descending Then
                          Direction = -1
                      ElseIf so = SortOrder.Ascending Then
                          Direction = 1
                      End If
                  End Sub
                  Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer \_
                  Implements System.Collections.IComparer.Compare
                      Dim RowOne As DataGridViewRow = CType(x, DataGridViewRow)
                      Dim RowTwo As DataGridViewRow = CType(y, DataGridViewRow)
                      Dim Result As Integer = \_
                      System.DateTime.Compare(RowOne.Cells(0).Value, RowTwo.Cells(0).Value)
                      Return Result \* Direction
                  End Function
              End Class
              

              NOTE: This sorts on column index(0). You can assign your column index to whatever you like by changing the value of x in the statement:

              System.DateTime.Compare(RowOne.Cells(x).Value, RowTwo.Cells(x).Value)

              Then after loading your data into the datagridview, add this line at the end:

              DataGridView1.Sort(New DataGridViewSortRowsByDateTime(SortOrder.Ascending))

              for ascending or:

              DataGridView1.Sort(New DataGridViewSortRowsByDateTime(SortOrder.Descending))

              I hope that helps (it did for me!)

              modified on Friday, November 20, 2009 3:50 AM

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups