DataGrid date sorting [modified]
-
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
-
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
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 ?
-
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
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[]
-
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 ?
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.
-
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[]
DataGridView control is in Framework 2.0+, not in 1.1 with which I´m developing this App.
-
DataGridView control is in Framework 2.0+, not in 1.1 with which I´m developing this App.
Sorry :sigh:
-
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.
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#
-
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#
Much easier. Thanx! :thumbsup: