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. C#
  4. DataGrid date sorting [modified]

DataGrid date sorting [modified]

Scheduled Pinned Locked Moved C#
csharpwinformsalgorithmstutorialquestion
8 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.
  • W Offline
    W Offline
    W1SHM4ST3R
    wrote on last edited by
    #1

    I´m developing a Windows Forms app with C# 2003. Well, I´ve got a DataGrid control with 2 Date columns with "dd/MM/yyyy" format...the trouble is that when I click that columns' headers they get obviously sorted by day, then month, and then year (01-02...30,31)...and I want them to get sorted by year, then month and then day...I´ve placed this code on the datagrid_mouseup event...so when the datagrid sorts the column it will do it correctly...but I don´t know how to revert back the date format, because the sorting method seems to be internal and performed after all other DataGrid´s events are processed. So how could I know when DataGrid has finished sorting to put the dates into their former format? (or other way of doing it, of course). Thanx. <pre> DataGrid.HitTestInfo hitTestInfo = myDataGrid2.HitTest(myDataGrid2.PointToClient(Control.MousePosition));                               if (hitTestInfo.Type==DataGrid.HitTestType.ColumnHeader)                {                     if ((hitTestInfo.Column==2)||(hitTestInfo.Column==3))                     {                          for (int i=0;i<Avisos.Rows.Count;i++)                          {                               DateTime myDateTime = DateTime.ParseExact(myDataGrid2[i,hitTestInfo.Column].ToString(),"dd/MM/yyyy",null);                               myDataGrid2[i,hitTestInfo.Column] = myDateTime.ToString("yyyy/MM/dd");                               Application.DoEvents();                          }            &nb

    N Y 2 Replies Last reply
    0
    • W W1SHM4ST3R

      I´m developing a Windows Forms app with C# 2003. Well, I´ve got a DataGrid control with 2 Date columns with "dd/MM/yyyy" format...the trouble is that when I click that columns' headers they get obviously sorted by day, then month, and then year (01-02...30,31)...and I want them to get sorted by year, then month and then day...I´ve placed this code on the datagrid_mouseup event...so when the datagrid sorts the column it will do it correctly...but I don´t know how to revert back the date format, because the sorting method seems to be internal and performed after all other DataGrid´s events are processed. So how could I know when DataGrid has finished sorting to put the dates into their former format? (or other way of doing it, of course). Thanx. <pre> DataGrid.HitTestInfo hitTestInfo = myDataGrid2.HitTest(myDataGrid2.PointToClient(Control.MousePosition));                               if (hitTestInfo.Type==DataGrid.HitTestType.ColumnHeader)                {                     if ((hitTestInfo.Column==2)||(hitTestInfo.Column==3))                     {                          for (int i=0;i<Avisos.Rows.Count;i++)                          {                               DateTime myDateTime = DateTime.ParseExact(myDataGrid2[i,hitTestInfo.Column].ToString(),"dd/MM/yyyy",null);                               myDataGrid2[i,hitTestInfo.Column] = myDateTime.ToString("yyyy/MM/dd");                               Application.DoEvents();                          }            &nb

      N Offline
      N Offline
      Natza Mitzi
      wrote on last edited by
      #2

      HI, Your code looks evil. Application.DOEvents() is a bad call. Re-think and do this in a different way and not with a hit test ?? How much data is on this grid ?

      Natza Mitzi Analysis Studio Statistical Analysis Software

      W 1 Reply Last reply
      0
      • W W1SHM4ST3R

        I´m developing a Windows Forms app with C# 2003. Well, I´ve got a DataGrid control with 2 Date columns with "dd/MM/yyyy" format...the trouble is that when I click that columns' headers they get obviously sorted by day, then month, and then year (01-02...30,31)...and I want them to get sorted by year, then month and then day...I´ve placed this code on the datagrid_mouseup event...so when the datagrid sorts the column it will do it correctly...but I don´t know how to revert back the date format, because the sorting method seems to be internal and performed after all other DataGrid´s events are processed. So how could I know when DataGrid has finished sorting to put the dates into their former format? (or other way of doing it, of course). Thanx. <pre> DataGrid.HitTestInfo hitTestInfo = myDataGrid2.HitTest(myDataGrid2.PointToClient(Control.MousePosition));                               if (hitTestInfo.Type==DataGrid.HitTestType.ColumnHeader)                {                     if ((hitTestInfo.Column==2)||(hitTestInfo.Column==3))                     {                          for (int i=0;i<Avisos.Rows.Count;i++)                          {                               DateTime myDateTime = DateTime.ParseExact(myDataGrid2[i,hitTestInfo.Column].ToString(),"dd/MM/yyyy",null);                               myDataGrid2[i,hitTestInfo.Column] = myDateTime.ToString("yyyy/MM/dd");                               Application.DoEvents();                          }            &nb

        Y Offline
        Y Offline
        Yuri Vital
        wrote on last edited by
        #3

        What about using a DataGridView Control instead ? This control have the Method "Sort" who accept a IComparer Interface. Just look at this How to: Customize Sorting in the Windows Forms DataGridView Control[]

        W 1 Reply Last reply
        0
        • N Natza Mitzi

          HI, Your code looks evil. Application.DOEvents() is a bad call. Re-think and do this in a different way and not with a hit test ?? How much data is on this grid ?

          Natza Mitzi Analysis Studio Statistical Analysis Software

          W Offline
          W Offline
          W1SHM4ST3R
          wrote on last edited by
          #4

          Forget that order. I´put it there just to place a breakpoint over it...but...why Application.Doevents is "evil" as you say? If I found a better option (or an option that could work, at least) be sure that I will take it :D. We are talking about 200 records more or less.

          N 1 Reply Last reply
          0
          • Y Yuri Vital

            What about using a DataGridView Control instead ? This control have the Method "Sort" who accept a IComparer Interface. Just look at this How to: Customize Sorting in the Windows Forms DataGridView Control[]

            W Offline
            W Offline
            W1SHM4ST3R
            wrote on last edited by
            #5

            DataGridView control is in Framework 2.0+, not in 1.1 with which I´m developing this App.

            Y 1 Reply Last reply
            0
            • W W1SHM4ST3R

              DataGridView control is in Framework 2.0+, not in 1.1 with which I´m developing this App.

              Y Offline
              Y Offline
              Yuri Vital
              wrote on last edited by
              #6

              Sorry :sigh:

              1 Reply Last reply
              0
              • W W1SHM4ST3R

                Forget that order. I´put it there just to place a breakpoint over it...but...why Application.Doevents is "evil" as you say? If I found a better option (or an option that could work, at least) be sure that I will take it :D. We are talking about 200 records more or less.

                N Offline
                N Offline
                Natza Mitzi
                wrote on last edited by
                #7

                Hi, As you can see in other responses, Application.DoEvents consumes all kinds of events such as user clicking buttons and can cause unexpected application states. It can also be a very heavy call to make in a loop, since you do not really know what or how much code will be executed. I am not saying never use it but use with extra caution (try not to use it). I asked for the number of records since you can use a list view in which sorting is very simple. Sorting list view C#

                Natza Mitzi Analysis Studio Statistical Analysis Software

                W 1 Reply Last reply
                0
                • N Natza Mitzi

                  Hi, As you can see in other responses, Application.DoEvents consumes all kinds of events such as user clicking buttons and can cause unexpected application states. It can also be a very heavy call to make in a loop, since you do not really know what or how much code will be executed. I am not saying never use it but use with extra caution (try not to use it). I asked for the number of records since you can use a list view in which sorting is very simple. Sorting list view C#

                  Natza Mitzi Analysis Studio Statistical Analysis Software

                  W Offline
                  W Offline
                  W1SHM4ST3R
                  wrote on last edited by
                  #8

                  Much easier. Thanx! :thumbsup:

                  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