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
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
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.
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
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>
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
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
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
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.
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
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
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
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.
- http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-binding/understanding-data-binding/server-side-binding/advanced-data-binding-(using-needdatasource-event)
- http://demos.telerik.com/aspnet-ajax/grid/examples/data-binding/simple-vs-advanced/defaultcs.aspx
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