Search code examples
c#asp.netgridviewtemplatefield

Gridview - Manipulating a data item in TemplateField


I'm displaying four data fields in a single column of a gridview using template field. One of the data fields is a date field. Instead of showing normal date, I want to display relative hours/days. (like: 2 hours ago/ 2 days ago). How can i manipulate the date field inside the template field column. Please suggest.

Thanks.


Solution

  • You can use <%# Eval("Date Field") %> to manipulate the data that is bound to the column at runtime. You can even call functions in your code-behind by doing something like this:

    <%# this.MyFunction(Eval("Date Field Name")) %>
    

    There may be a better way to do it, but this has worked for me in the past. Note that Eval returns an object.