show date instead of DateTime in telerik Grid

TRR picture TRR · Apr 25, 2012 · Viewed 9.4k times · Source

i am using telerik grid in my Asp.Net MVC3 application and i want to bind a Date of format dd/mm/yyyy to one of the columns of the grid as shown below:

Html.Telerik().Grid<TestResults>()
    .Name("TestResultGrid")
    .DataKeys(keys => keys.Add(c => c.SourceProgramId))
    .Columns(columns =>
        {
            columns.Bound(c => c.OriginalSourceProgramId).Title("Original Case ID").Width("90").Visible(IsOriginalCaseIdVisible);
            columns.Bound(c => c.SourceProgramId).Title("Source Program ID").Width  ("90").Visible(!IsOriginalCaseIdVisible);
           columns.Bound(c => c.Name).Title("Name").Width("140");
            columns.Bound(c => c.Points).Title("Points").Width("50");
            columns.Bound(c => c.ProgramName).Title("Program").Width("80");
            columns.Bound(c => c.DOB).Title("Created Date").Width("80");
        })
    .DataBinding(dataBinding =>
            dataBinding.Ajax()
                .Select("_TestResutls", "Test"))
    .ClientEvents(events => events
    .OnDataBinding("Grid_onDataBinding")
    .OnDataBound("Grid_onDataBound")
    .OnRowDataBound("onRowDataBound")
    .OnRowSelect("onRowSelected"))
    .Pageable(paging => paging.PageSize(10))
    .NoRecordsTemplate("<b>No Records to display.<b>")
    .Sortable()
    .Selectable()
    .HtmlAttributes(new { @class = "grid_table" })
    .Footer(true)
    .Render();

The dataType of DOB is DateTime?. I want to display date of format dd/mm/yyyy. i could have converted it to ToString() to achieve this, But i have sorting on this column. When i sort by this column it is treating the values as string and not returning the expected results. This column is nullable and hence i cant use DOB.Date method also. Any help?

Answer

Chris Surfleet picture Chris Surfleet · Apr 25, 2012

You should be able to do columns.Bound(c => c.DOB).Format("{0:dd/MM/yyyy}").Title("Created Date").Width("80");

Hope that helps