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

DataTextFormatString for dates

1 Answer 344 Views
Grid
This is a migrated thread and some comments may be shown as answers.
christina noel
Top achievements
Rank 1
christina noel asked on 05 May 2011, 05:28 PM
Hello,

We are using the following column definition for a button column that should display a DateTime:

<telerik:GridButtonColumn UniqueName="STARTDATE" ButtonType="LinkButton"
       DataTextField="STARTDATE" DataTextFormatString="{0:g}" HeaderText="[START DATE]"
       CommandName="GoToItem" />

The problem is that it is standard in our app to display just the date if the time is "12:00 am", as this usually indicates that the exact time was not entered. Therefore, I need to conditionally use "{0:d}" for my format string instead of "{0:g}" when StartDate.TimeOfDay.Ticks=0.

We have a server-side function with the following format that we usually use for this logic:
public static string GetShortDateTimeString(DateTime? aDateTime)

In the past, we've either called the function in the ItemDataBound event or used a GridTemplateColumn with a LinkButton to call the function:
<telerik:GridTemplateColumn HeaderText="[START DATE]"DataField="STARTDATE" UniqueName="STARTDATE">
    <ItemTemplate>
      <asp:LinkButton ID="lnkSTARTDATE" runat="server"
            Text='<%#GetShortDateTimeString((DateTime?)Eval("STARTDATE"))%>'
            OnCommand="lnkViewGrid_Command" CommandArgument='<%# Eval("OID").ToString() %>'/>
    </ItemTemplate>
     </telerik:GridTemplateColumn>

My question is whether there is something I can do in the GridButtonColumn definition to create this complex formatting logic, or if I have to use one of those two options. I tried using the GridButtonColumn "Text" field but got an error when I tried to load the page:

Databinding expressions are only supported on objects that have a DataBinding event. Telerik.Web.UI.GridButtonColumn does not have a DataBinding event.


--Christina

1 Answer, 1 is accepted

Sort by
0
Accepted
Marin
Telerik team
answered on 11 May 2011, 08:55 AM
Hi Christina,

Such conditional formatting of the datetime cannot be achieved only through the markup so you will need some server side logic in order to accomplish this. The ItemDataBound event is a good place to call the custom function and format the column text in a appropriate way.

Regards,
Marin
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
christina noel
Top achievements
Rank 1
Answers by
Marin
Telerik team
Share this question
or