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

[Solved] GridDateTimeColumn sorting bug with nested object

4 Answers 278 Views
Grid
This is a migrated thread and some comments may be shown as answers.
bkelts
Top achievements
Rank 1
bkelts asked on 24 Apr 2009, 10:00 PM

When a GridDateTimeColumn is bound to a nested object (e.g. DataField="SomeObject.DateField"), column sorting is broken. The sorting appears to be based on the alphanumeric order. Here is the test code below with a workaround and a workaround attempt that causes an exception:

default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server">  
        <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
        <div> 
            <telerik:RadGrid AllowSorting="true" AllowPaging="true" PageSize="25" ID="RadGrid1" runat="server"   
                OnNeedDataSource="RadGrid1_NeedDataSource">  
                <MasterTableView AutoGenerateColumns="false">  
                    <Columns> 
                        <telerik:GridDateTimeColumn   
                            DataFormatString="{0:MM/dd/yyyy}" 
                            DataField="RefDate"   
                            UniqueName="Column0"   
                            HeaderText="ParentObj">  
                        </telerik:GridDateTimeColumn> 
                        <telerik:GridDateTimeColumn   
                            DataFormatString="{0:MM/dd/yyyy}" 
                            DataField="Dobj.RefDate1"   
                            UniqueName="Column1"   
                            HeaderText="ChildObj">  
                        </telerik:GridDateTimeColumn> 
                        <telerik:GridTemplateColumn  
                            SortExpression='Dobj.RefDate1.ToString("s")' 
                            UniqueName="Column2"   
                            HeaderText="WorkAround1">  
                            <ItemTemplate> 
                                <asp:Label ID="Label1" runat="server"   
                                    Text='<%# String.Format("{0:MM/dd/yyyy}", Eval("Dobj.RefDate1")) %>' 
                                > 
                                </asp:Label> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridDateTimeColumn  
                            SortExpression='Dobj.RefDate2.ToString("s")' 
                            DataFormatString="{0:MM/dd/yyyy}" 
                            DataField="Dobj.RefDate2"   
                            UniqueName="Column3"   
                            HeaderText="WorkAround2">  
                        </telerik:GridDateTimeColumn>                    
                    </Columns> 
                </MasterTableView> 
            </telerik:RadGrid> 
        </div> 
    </form> 
</body> 
</html> 
 

default.aspx.cs:

using System;  
using System.Data;  
using System.Configuration;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
using System.Collections.Generic;  
 
public partial class _Default : System.Web.UI.Page   
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
 
    }  
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
    {  
        RadGrid1.DataSource = GetDataList();  
    }  
 
    protected List<PObject> GetDataList()  
    {  
        DateTime refdt = new DateTime(2008, 12, 20);  
        var dlist = new List<PObject>();  
        for (int i = 1; i < 20; i++)  
        {  
           var dt = refdt.AddDays(i);  
           dlist.Add(   
                        new PObject   
                        {  
                            Dobj = new DObject { RefDate1 = dt, RefDate2 = dt },  
                            RefDate = dt  
                        }  
                    );  
        }  
        return dlist;  
    }  
}  
 
public class PObject  
{  
    public DObject Dobj { getset; }  
    public DateTime RefDate { getset; }  
}  
 
public class DObject  
{  
    public DateTime RefDate1 { getset; }  
    public DateTime RefDate2 { getset; }  

You will notice that the first column "ParentObj" sorts fine since the bound property is part of the data object. The second column "ChildObj" is bound to a nested/child object of the main data object. This one does not sort properly. The third column "WorkAround1" is a way to work around the problem using a GridTemplateColumn and a SortExpression using .ToString("s"). Since using a GridTemplateColumn is kind of cumbersome, I thought I'd try adding the .ToString("s") to the SortExpression of a GridDateTimeColumn, but this causes an exception. This is demonstrated when trying sort the "WorkAround2" column. Although this exception should probably fixed anyway (it doesn't make sense that it works for a GridTemplateColumn and not any other kind of bound column), getting the grid to support sorting on a DateTime property of a nested object either way is fine with me.

4 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 27 Apr 2009, 04:29 PM
Hello Brett,

The SortExpression property of the column expects a data field and cannot be assigned to a string value. To avoid the abnormality you described so far you need to set a date type for the column. Here is a code snippet showing how to achieve this:
                        <telerik:GridDateTimeColumn    
                            DataFormatString="{0:MM/dd/yyyy}"  
                            DataField="Dobj.RefDate1"    
                            UniqueName="Column1" 
                            DataType="System.DateTime" 
                            SortExpression="Dobj.RefDate1" 
                            HeaderText="ChildObj">   
                        </telerik:GridDateTimeColumn>  

Regards,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
bkelts
Top achievements
Rank 1
answered on 27 Apr 2009, 10:54 PM
OK. That worked, but it seems a silly requirement to have to specify the DataType property when the control itself ("GridDateTypeColumn") already implies the data type.
0
Georgi Krustev
Telerik team
answered on 28 Apr 2009, 03:01 PM
Hello Brett,

This is required because the column is bound to a nested object. Hence you need to specify the type in order the grid to know how to handle the data.

Regards,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Mohan Bhandari
Top achievements
Rank 1
answered on 22 Dec 2009, 11:30 PM
Thanks I was able to resolve it.
Tags
Grid
Asked by
bkelts
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
bkelts
Top achievements
Rank 1
Mohan Bhandari
Top achievements
Rank 1
Share this question
or