Solved

How to format a datetime boundfield in codebehind

Posted on 2009-06-30
3
1,147 Views
Last Modified: 2012-05-07
I'm trying to format a datetime field in a gridview from code-behind (this is for a SharePoint WSP so I don't have an ASPX Page to work from) but my format is being ignored. I'm trying to accomplish 2 things, remove military time format from the Boundfield and format the date in this format "{0:MM/dd/yyyy HH:ss}"
BoundField modifiedField = new BoundField();
            modifiedField.HtmlEncode = false;
            modifiedField.HeaderText = "Modified";
            modifiedField.DataField = "Modified";
            modifiedField.ApplyFormatInEditMode = true;
            modifiedField.DataFormatString = "{0:MM/dd/yyyy HH:ss}";
            tasksCollection.Columns.Add(modifiedField);

Open in new window

0
Comment
Question by:kvigor
  • 2
3 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 24747823
You will need to check the underlying data field and verify that its data type is a date/time. If the data type is string, the data format string will not work, as it only works on dates, numbers, enumerators and boolean data types. BTW, was your intention to not display minutes?
0
 

Author Comment

by:kvigor
ID: 24747988
No, my error about the minutes, I'll GeType and check. Meantime this is how the GridView Returns the field without any formating...
Gridview.jpg
0
 

Accepted Solution

by:
kvigor earned 0 total points
ID: 24764256
OK
This didn't work:
                    if (row["modified"].GetType().Equals("DateTime"))
                    {
                        row["modified"] = row["modified"].ToString() + "isString";
                    }
This did work:
 row["modified"] = DateTime.Parse(row["modified"].ToString()).ToShortDateString() + "<br />" + DateTime.Parse(row["modified"].ToString()).ToShortTimeString();
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio hangs on running project 6 55
using CK editor in iframes 5 26
Return array 3 19
How to fix 'event logs could not be searched' error asp.net? 3 11
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question