This is a migrated thread and some comments may be shown as answers.

DateTime column Sorting problem - Please Urgent

12 Answers 468 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Manish
Top achievements
Rank 2
Manish asked on 03 Jul 2013, 07:11 AM
Hi,
I have a column StartDate type Datetime. But when i click on column header for sorting it is not sorting properly in acs or desc. it is showing sorting only on month part of date
Like
08/21/2012
07/21/2014
06/26/2013
06/26/2013
It is in descending order but it is wrong.

<telerik:GridBoundColumn DataField="StartDate" HeaderText="Start Date" SortExpression="StartDate" DataType="System.DateTime"
UniqueName="StartDate"DataFormatString="{0:MM/dd/yyyy}" ReadOnly="true"
 ItemStyle-Width="80px" HeaderStyle-Width="80px">
</telerik:GridBoundColumn>
I have tried this http://www.telerik.com/community/forums/aspnet-ajax/grid/sort-the-date-column-in-radgrid-on-clicking-of-the-header.aspx but still no success. Please find screen shot of problem.

12 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 03 Jul 2013, 07:20 AM
Hi Manish,

I tried to replicate the issue,but it works fine at my end. Can you try using GridDateTimeColumn instead of GridBoundColumn.
I have tried using both and the sorting takes place fine.

ASPX:
<telerik:GridDateTimeColumn DataField="StartDate" HeaderText="StartDate" SortExpression="StartDate"
DataType="System.DateTime" UniqueName="StartDate" DataFormatString="{0:MM/dd/yyyy}"
ReadOnly="true" ItemStyle-Width="80px" HeaderStyle-Width="80px">
</telerik:GridDateTimeColumn>

Thanks,
Princy
0
Manish
Top achievements
Rank 2
answered on 03 Jul 2013, 07:31 AM
Hi Princy,

Thanks for your quick reply. I have applied your code
<telerik:GridDateTimeColumn DataField="StartDate" HeaderText="StartDate" SortExpression="StartDate"
DataType="System.DateTime" UniqueName="StartDate" DataFormatString="{0:MM/dd/yyyy}"
ReadOnly="true" ItemStyle-Width="80px" HeaderStyle-Width="80px">
</telerik:GridDateTimeColumn>

But still it is not working. When my data come from database and i have not apply any sorting from grid than it is working fine but when i am going to apply from column it shows wrong data. Please help still not working fine with your code as well.
0
Viktor Tachev
Telerik team
answered on 05 Jul 2013, 12:04 PM
Hello Manish,

I tried replicating the scenario displayed on the screenshot but to no avail. I used the same settings for the GridBoundColumn as the ones you have set. When the RadGrid is sorted it produces the expected result on my end - the whole date is taken into account. You would find a sample project attached. Give it a try and see if it is working as expected on your end.

If you have custom sorting for the column, try disabling it and see if the result is different.

Regards,
Viktor Tachev
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Shafqat
Top achievements
Rank 1
answered on 24 Nov 2016, 05:54 PM

i have sorting issue in Telerik radgrid on Date column, below is my code for radgrid 

     <telerik:radgrid id="gdshow" runat="server" width="100%" allowsorting="true" allowfilteringbycolumn="false"     OnNeedDataSource="gdshow_NeedDataSource"     OnItemCommand="gdshow_ItemCommand"
        OpenInNewWindow="true"     pagesize="20" allowmultirowselection="false" allowpaging="True" showgrouppanel="false" OnPageIndexChanged="gdshow_PageIndexChanged" OnPageSizeChanged="gdshow_PageSizeChanged" OnSortCommand="gdshow_SortCommand"
            gridlines="None" style="border: 0; outline: none;" itemstyle-forecolor="Black" headerstyle-font-bold="true" headerstyle-horizontalalign="left"
            itemstyle-horizontalalign="Center" headerstyle-forecolor="Black" ExportSettings-FileName="Custom Report" OnGridExporting="gdshow_GridExporting" ExportSettings-IgnorePaging="true">
                     <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
                                                    <MasterTableView  CommandItemDisplay="Top" AllowMultiColumnSorting="true" PagerStyle-AlwaysVisible="true">
                                                        <ItemStyle CssClass="GridRow1" />
                                                       <AlternatingItemStyle CssClass="GridRow2" />  
                                                      
                                                                <%-- <PagerStyle AlwaysVisible="true" ShowPagerText="false"      FirstPageImageUrl="../images/First.png" PrevPageImageUrl="../images/Per.png" NextPageImageUrl="../images/Next.png"  LastPageImageUrl="../images/Last.png" PageSizeLabelText="Show:" />--%>

                                                        <CommandItemTemplate>
                                                        </CommandItemTemplate>

                                                          </MasterTableView>
                                                         <ClientSettings>
                                                      <ClientEvents OnGridCreated="GridCreated" />
                                                 </ClientSettings>                                 
                                            
                    </telerik:radgrid>

0
Viktor Tachev
Telerik team
answered on 25 Nov 2016, 12:41 PM
Hi Shafqat,

Would you ensure that the field for the column showing the Date is actually a DateTime?

If that is the case please send us a sample where the issue is replicated.


Regards,
Viktor Tachev
Telerik by Progress
Telerik UI for ASP.NET AJAX is ready for Visual Studio 2017 RC! Learn more.
0
Shafqat
Top achievements
Rank 1
answered on 25 Nov 2016, 02:16 PM

Hi Viktor Tachev,
Actually i used auto generated columns in telerik radgrid, and the field for the column showing the Date is surely a DateTime.

it sorts with respect to day, not w.r.t year, i want to sort w.r.t year.

in above post i showed my radgrid code and i am going to attache a screen shot of grid showing sorted data of Date Field

0
Viktor Tachev
Telerik team
answered on 30 Nov 2016, 09:17 AM
Hello Shafqat,

I created a sample project with RadGrid that has autogenerated columns. The dates in the sample are sorted as expected on my end. Please give the sample a try and let me know how it works for you.

I would appreciate it if you can modify the sample in a way that the behavior you are seeing is replicated and send it back. This way we can examine the issue and look for its cause.

Regards,
Viktor Tachev
Telerik by Progress
Telerik UI for ASP.NET AJAX is ready for Visual Studio 2017 RC! Learn more.
0
Shafqat
Top achievements
Rank 1
answered on 30 Nov 2016, 07:10 PM

Hi Viktor Tachev

Thank you so much. my issue is fixed now, actually i was converting the date column to Varchar through SQL Function Convert(varchar(10),startDate,101) before Binding to Grid, that's why sorting was not working correctly. Now i am converting date column to Date with SQL Function CAST(DoBirth AS DATE) before binding to Grid.

Once again Thank you so much for responding me.

0
Shafqat
Top achievements
Rank 1
answered on 28 Dec 2016, 02:43 PM

Hi Viktor Tachev
i have sorting issue in Telerik radgrid on date column(date of birth), but this time the data type of that column is varchar because i am saving encrypted date in column(date of birth). Now how my date column will be sorting.

 

Thank you

0
Viktor Tachev
Telerik team
answered on 29 Dec 2016, 01:00 PM
Hello,

If the data in the column is converted to varchar it will be sorted as a string. In order for the column to be sorted as a date you need to ensure that you pass the data as Date type.

Regards,
Viktor Tachev
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Shafqat
Top achievements
Rank 1
answered on 19 Jan 2017, 06:05 PM

Thank you for reply
But my date is saved in encrypted form in column, so definitely its type will be varchar. in my code i use a dll which decrypt (decryption) the date, but after decryption the type of date will be varchar definitely. So is there any way at binding of this column with RadGrid to convert to date type. following is my code which binds the date column with RadGrid.

if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
                {
                    GridDataItem dataBoundItem = e.Item as GridDataItem;

                    foreach (GridColumn column in dataBoundItem.OwnerTableView.RenderColumns)
                    {
                        if (column.HeaderText.Trim().ToLower().Contains("date"))
                        {   
                            dataBoundItem[column.UniqueName].Text = dataBoundItem[column.UniqueName].Text.Replace(" 12:00:00 AM", "").Replace("1/1/1900", "");
                        }
                    }
                } 

 

 

Thank you

0
Viktor Tachev
Telerik team
answered on 20 Jan 2017, 03:02 PM
Hi Shafqat,

In order for the data to behave as Date you would need to convert it before passing it to the RadGrid control. Otherwise the Grid will sort the data as string.

You can use the NeedDataSource event for the grid for binding. In the handler you can retrieve the data from the database and convert the relevant columns to the corresponding type. Then, you should pass the converted data to RadGrid.

Please examine the following resources that describe in more detail how you can bind the grid via NeedDataSource.


For your convenience I am attaching a sample project which outlines the approach you can use to convert the data before passing it to the Grid. Note that this is a basic example. You would need to adjust it based on your specific requirements. However, it should get you started.

Regards,
Viktor Tachev
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Manish
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 1
Manish
Top achievements
Rank 2
Viktor Tachev
Telerik team
Shafqat
Top achievements
Rank 1
Share this question
or