Solved

How to format a datetime boundfield in codebehind

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

21 Experts available now in Live!

Get 1:1 Help Now