Alejandro B.
Alejandro B.

Reputation: 5092

GridView date/time column conditional formatting

I have a GridView with a DateTime column. My dates may not have a time part. Using DataFormatString="{0:MM/dd/yyyy hh:mm tt}" will write 12:00 AM when no time was entered. Is there a way to avoid this? That is, if the date has a time setted, then show it, otherwise show just the date?

Upvotes: 1

Views: 1085

Answers (2)

Brian Mains
Brian Mains

Reputation: 50728

You can't use a conditional with a format string; you have to add an event handler to the GridView's ItemDataBound event, and format it yourself:

void Grid_ItemDataBound(object sender, GridItemEventArgs e)
{
   var date = e.Item[<index>].Text;
   if (date.EndsWith(" 12:00 AM"))
       e.Item[<index>].Text = date.Replace(" 12:00 AM", "");
}

Something like that.

Upvotes: 0

Imran Balouch
Imran Balouch

Reputation: 2170

If there is no special restriction, than from server side you can convert the date time to string and return the required data and on grid side just tell the grid that it is a string value.

Upvotes: 1

Related Questions