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

Specify datatype of column or sort string date

3 Answers 152 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jay
Top achievements
Rank 1
Jay asked on 30 Jul 2012, 05:15 PM
I have a column which contains a date, but I need to return it from my C# model as string so that i can do some special formatting to it first (I need to check for null and for DateTime.Min and then return an empty string in those instances). If it's a string, however, I understand that the sorting won't work correctly. Is there a way to set the data type of the column to DateTime or to get the string date to sort correctly?

Thanks,
Jay

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 31 Jul 2012, 05:18 AM
Hi Jay,

I guess your requirements are follows.

1.Format the date in code behind. I tried accessing the date from a GridBoundColumn.

ASPX:
<telerik:GridBoundColumn UniqueName="OrderDate" DataField="OrderDate" DataType="System.DateTime"></telerik:GridBoundColumn>

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem ditm = (GridDataItem)e.Item;
        string date = ditm["OrderDate"].Text;
        if (dt == "your condition") // checking for condition
            {
                ditm["OrderDate"].Text = " ";
            }
    }
}

2.To sort a Field

C#:
protected void Page_Load(object sender, EventArgs e)
{
 
    GridSortExpression ge = new GridSortExpression();
    ge.FieldName ="OrderDate";
    ge.SortOrder =GridSortOrder.Ascending; //sorting in Ascending order
    RadGrid1.MasterTableView.SortExpressions.Add(ge);
    RadGrid1.Rebind();            
}

Please provide the code if this doesn't help.

Thanks,
Shinu.
0
Jay
Top achievements
Rank 1
answered on 31 Jul 2012, 01:13 PM
I have to apologize. I may have posted this question in the wrong forum. I'm using the grid in MVC.
0
Jay
Top achievements
Rank 1
answered on 31 Jul 2012, 02:31 PM
This seems to be working okay. What do you think?

columns.Bound(p => p.Date).ClientTemplate("<#= checkDate(Date) #>").Title(Html.LabelFor(l => l.Sales.Date).ToHtmlString());<br>

<
script type="text/javascript">
    function checkDate(d)
     {
        var minDate = new Date();
        minDate.setFullYear(1, 0, 1);
        minDate.setHours(0, 0, 0, 0);


        if (d == null || d == minDate)
            return "";
        else
            return d.toString("@MyConfiguration.Current.DateFormat");
    }
</script>
Tags
Grid
Asked by
Jay
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Jay
Top achievements
Rank 1
Share this question
or