Solved

How to format a datetime boundfield in codebehind

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

734 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