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

Sort the date column in radgrid on clicking of the header

3 Answers 1101 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Akki
Top achievements
Rank 1
Akki asked on 22 May 2012, 10:18 AM
Hi All,

In my page I have date column inside my telerik radgrid. On clicking of the column header the date is not sorting properly. Tag is like this -

<telerik:GridBoundColumn DataField="date" HeaderText="Date" UniqueName="date"
         ReadOnly="true" AllowSorting="true" DataType="System.DateTime"
                        DataFormatString="{0:MM/dd/yyyy}" SortExpression="date"/>

The date is sorting only from month not from  year, month and date. In the database the data type is smalldatetime. While retriving from database, I am using  'convert(char(10),GETDATE(),101)' .
The result I am getting is
12/30/2011
12/28/2011
12/20/2011
11/30/2011
1/04/2012
It should be
11/30/2011
12/20/2011
12/28/2011
12/30/2011
1/04/2012

Thanks in Advance.
Akki

3 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 24 May 2012, 03:59 PM
Hello Akki,

I have attached a sample RadGrid page where I implemented sorting to several datetime columns with different formats:
  • System.DateTime
  • System.TimeSpan
  • System.String

Please note the difference between the sorting of DateTime values and String values. In your case your datafield probably is generating its values as char[] type ( strings ). Please try to parse these values to DateTime format.

I hope this helps. Let me know if you need further assistance.

All the best,

Eyup
the Telerik team
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 their blog feed now.
0
Keshab
Top achievements
Rank 1
answered on 31 Mar 2014, 05:21 PM
This tag does not work. Please see the screen shot.
I have date increasing for 2014. All of sudden 2013 showed up in the column.

<
telerik: GridDateTimeColumn

UniqueName="UpdateDateShort"

DataField="UpdateDateShort"

HeaderText="Updated"

AllowSorting="true"

ShowSortIcon="true"

DataType="System.DateTime" DataFormatString="{0:MM/dd/yyyy}" ReadOnly="true"

FilterControlAltText="Filter Updated column">

</telerik: GridDateTimeColumn>

0
Princy
Top achievements
Rank 2
answered on 01 Apr 2014, 06:13 AM
Hi Keshab,

I tried your code but was not able to replicate such an issue. Here is a sample code snippet that I tried.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID" />
            <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name" />
            <telerik:GridDateTimeColumn UniqueName="Date" DataField="Date" HeaderText="Date" AllowSorting="true" ShowSortIcon="true" DataType="System.DateTime" DataFormatString="{0:MM/dd/yyyy}" ReadOnly="true">
            </telerik:GridDateTimeColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    dynamic data = new[] {
new { ID = 1, Name = "Name1", Date="01/22/2014"},
new { ID = 2, Name = "Name2", Date="01/23/2014"},
new { ID = 3, Name = "Name3", Date="02/20/2014"},
new { ID = 4, Name = "Name4", Date="02/21/2014"},
new { ID = 5, Name = "Name5", Date="10/23/2013"},
new { ID = 6, Name = "Name6", Date="03/25/2014"},
new { ID = 7, Name = "Name7", Date="02/20/2014"},
new { ID = 8, Name = "Name8", Date="10/22/2013"},
new { ID = 9, Name = "Name9", Date="10/23/2013"}
};
    RadGrid1.DataSource = data;
}

Thanks,
Princy
Tags
Grid
Asked by
Akki
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Keshab
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or