Solved

How to format a datetime boundfield in codebehind

Posted on 2009-06-30
3
1,139 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now