Sort the date column in radgrid on clicking of the header

4 posts, 0 answers
  1. Akki
    Akki avatar
    79 posts
    Member since:
    Nov 2011

    Posted 22 May 2012 Link to this post

    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
  2. Eyup
    Admin
    Eyup avatar
    4010 posts

    Posted 24 May 2012 Link to this post

    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.
  3. Keshab
    Keshab avatar
    5 posts
    Member since:
    Jan 2014

    Posted 31 Mar 2014 Link to this post

    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>

  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 01 Apr 2014 in reply to Keshab Link to this post

    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
Back to Top