Articles in this section
Category / Section

How to bind TimeSpan value to Grid column in .NET MVC Grid ?

1 min read

We cannot bind the Timespan value directly to the grid columns. This is a known limitation due to the fact that there is no Timespan datatype in Javascript. Also the .Net JavascriptSerailizer will serializes the TimeSpan object to Complex object, and hence trying to bind the Timespan value directly in grid columns will display the column value as [object Object].

In such cases, we can use the Column template feature of the grid to display the TimeSpan values in desired format.

For example we can use the following Jsrender custom converter to format the timespan value.

 
<script type="text/javascript">
 
        $.views.converters("value", function (val) {
 
            return Globalize.format(val, "D2");
 
        });
 
    </script>
 

 

In the following code snippets, we have demonstrated, how to use the column template to display the timespan value in grid.

Grid Initialization

JS

 
<div id="Grid"></div>
 
<script type="text/javascript">
 
$("#Grid").ejGrid({
            dataSource: ej.DataManager({ url:"Home/GetEmployee", adaptor:"UrlAdaptor" }),
            allowPaging: true,           
            columns: [               
                      { field: "EmployeeID", headerText: 'Employee ID' },
                      { field: "FirstName", headerText: 'First Name'},
                      { field: "LastName", headerText: 'Last Name'},
                      { headerText: 'Worked Hours', template: "{{value:WorkedHours.Hours}} : {{value:WorkedHours.Minutes}} : {{value:WorkedHours.Seconds}}" },                     
            ]       
    });
 
</script>
 

 

MVC

 
@(Html.EJ().Grid<object>("Grid")
.Datasource(ds => ds.Url("Home/GetEmployee").Adaptor(AdaptorType.UrlAdaptor))
.AllowPaging()
.Columns(col =>
        {
            col.Field("EmployeeID").HeaderText("Employee ID").Add();
            col.Field("FirstName").HeaderText("First Name").Add();
            col.Field("LasttName").HeaderText("Last Name").Add();
            col.HeaderText("Worked Hours").Template("{{value:WorkedHours.Hours}} : {{value:WorkedHours.Minutes}} : {{value:WorkedHours.Seconds}}").Add();
 }))

ASPX

 
<ej:Grid ID="Grid" runat="server" AllowPaging="true">    
    <DataManager URL="Default.aspx/GetEmployee" Adaptor="UrlAdaptor" />       
    <Columns>
        <ej:Column Field="EmployeeID" HeaderText="Employee ID"/>
        <ej:Column Field="FirstName" HeaderText="First Name"/>
        <ej:Column Field="LastName" HeaderText="Last Name"/>
        <ej:Column HeaderText="Worked Hours" Template="{{value:WorkedHours.Hours}} : {{ value:WorkedHours.Minutes}} : {{value:WorkedHours.Seconds}}"/>
     </Columns>
</ej:Grid>
 

 

The grid data has been returned from the below function.

 

 
        public IEnumerable<Employee> GetEmployee()
        {
 
            return new List<Employee>() {
               new Employee(){ EmployeeID=1, FirstName="Gatsby", LastName="Great", WorkedHours= TimeSpan.Zero},
               new Employee(){ EmployeeID=2, FirstName="Rock", LastName="John", WorkedHours= TimeSpan.FromHours(9)},
               new Employee(){ EmployeeID=3, FirstName="Greak", LastName="Sena", WorkedHours= TimeSpan.FromHours(7)},
               new Employee(){ EmployeeID=4, FirstName="Bolt", LastName="Remo", WorkedHours= TimeSpan.FromHours(6)},
               new Employee(){ EmployeeID=5, FirstName="Alan", LastName="Sangeth", WorkedHours= TimeSpan.FromHours(5)},
               new Employee(){ EmployeeID=6, FirstName="Linga", LastName="Lee", WorkedHours= TimeSpan.FromHours(4)},
               new Employee(){ EmployeeID=7, FirstName="Frok", LastName="Reck", WorkedHours= TimeSpan.FromHours(3)},
            };
}

The output will be as follows.

C:\Users\madhup\Pictures\timespan[object].PNG

Figure 1: Grid with Timespan column without using template - see highlighted column

Figure 2: Grid with Timespan column using template - see highlighted column

Conclusion

I hope you enjoyed learning about how to bind TimeSpan value to Grid column in .NET MVC Grid.

You can refer to our .NET MVC Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied