Solved

Display Format Error

Posted on 2014-01-20
2
330 Views
Last Modified: 2014-01-21
Hi,

I have the below class with display format attribute for TimeSpan

where it has to display only hours and minutes

public partial class abc
    {
        [DataType(DataType.Time)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:hh\:mm}")]
        public System.TimeSpan getTimes
        {
            get;
            set;
        }      
     
    }


and now i am calling it from button click event as below

 private void button1_Click(object sender, EventArgs e)
        {
            System.TimeSpan d = new System.TimeSpan();
            abc  obj = new abc();
            obj.getTimes = d;
            MessageBox.Show(obj.getTimes.ToString());
        }

 here after executing it is showing the time with hours , minutes and seconds

 but i want to display it with only hours and minutes , i don't want seconds

 could you please suggest me what is the wrong in above code, I am using mvc4 Razor

                   Many thanks
0
Comment
Question by:praveen1981
2 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39796303
Hi,

DataFormatingString is for when you use the html helpers, like

    @Html.DisplayFor(m=>m.getTimes)

Open in new window


In your case, since you are using this in a messagebox, you should format it using the ToString:

MessageBox.Show(obj.getTimes.ToString(@"hh\:mm"));

Open in new window


Giannis
0
 

Author Closing Comment

by:praveen1981
ID: 39796363
Thanks.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

14 Experts available now in Live!

Get 1:1 Help Now