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

Grid Sort By Year Desc

1 Answer 24 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jamie
Top achievements
Rank 2
Jamie asked on 15 Oct 2013, 02:01 PM

The telerik grid I have uses sorting on every field. I wanted to sort by the date in descending order, but now it is making the data show up wrong because it is sorting by the month and day, not the month, day, and year.

Ex: 
12/31/2000
12/31/1998
12/31/1997
12/30/2012
12/30/2011

I would like the data to show up with the most recent month, day, and year...like this:

12/30/2012
12/30/2011
12/31/2000
12/31/1998
12/31/1997

<telerik:GridDateTimeColumn AllowFiltering="true" SortExpression="Service_Date" DataField="Service_Date" HeaderText="Service Date" UniqueName="Service_Date"  DataType="System.DateTime" DataFormatString="{0:MM/dd/yyyy}" ReadOnly="true" >

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 16 Oct 2013, 05:58 AM
Hello,

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
                AllowFilteringByColumn="true"  OnItemDataBound="RadGrid1_ItemDataBound"
                AllowSorting="true">
                <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID">
                    <Columns>
                        <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name" HtmlEncode="true"></telerik:GridBoundColumn>
                        <telerik:GridDateTimeColumn DataField="Customdate" DataFormatString="{0:yyyy}" UniqueName="Customdate" HeaderText="Customdate"></telerik:GridDateTimeColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable dt = new DataTable();
 
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Customdate", typeof(DateTime));
 
        dt.Rows.Add(1, "name1", DateTime.Now);
        dt.Rows.Add(2, "name2", DateTime.Now.AddYears(-1));
        dt.Rows.Add(3, "name3", DateTime.Now.AddYears(1));
 
        RadGrid1.DataSource = dt;
    }
 
     
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            DataRowView rv = item.DataItem as DataRowView;
            item["Customdate"].Text = Convert.ToDateTime(rv["Customdate"]).ToString("MM/dd/yyyy");
        }
    }


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Jamie
Top achievements
Rank 2
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or