?
Solved

How to format a datetime boundfield in codebehind

Posted on 2009-06-30
3
Medium Priority
?
1,155 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

770 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