Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

.NET DetailsView, getting a label displaying a float to display two leading zero's...

Options
  • 27-01-2011 3:04pm
    #1
    Closed Accounts Posts: 3,912 ✭✭✭


    Hi folks,

    I'm using a DetailsView in my .NET site to display some data, I've converted some fields to templates and all is working fine there...

    One of the fields uses a label to display a price, (stored in my DB as a float and handled in my codebehind as a float), but when displaying on my .aspx page, when the price is say "20.00", it is displayed as "20" and when the price is "21.68" it displays correctly as "21.68"...

    Is there any way I can handle the label in the codebehind to always display the price in the normal pricing format of "xx.yy"???

    I've tried:

    Label llb3 = this.DetailsView1.FindControl("Label3") as Label;
    llb3.Text = Prod.ToString("F2");

    But this only works when I use the edit button (as the label control is not built on PageLoad, only built when I go to edit the DetailsView data)...

    I'm trying to see if I can set up the properties of a DetailsView field to get my .yy data displaying correctly...

    Thanks in advance for any help with this...


Comments

  • Closed Accounts Posts: 3,912 ✭✭✭HellFireClub


    I've changed the data type of my column in my MS SQL 2005 DB from "float" to money and now I've the opposite problem of 4 leading zero's! I'm sure it's just redeclaring the money column with the required number of zero's and debuild my DB to resolve?!?


  • Registered Users Posts: 515 ✭✭✭NeverSayDie


    You can specify formatting in the databinding expression, eg if you've bound the Text property of your Label in the template;

    <asp:Label ID="lblMyLabel" runat="server" Text='<%# Eval("Price", "{0:C}") %>'/>

    The bolded bit would be the data format string - set to currency in this example. If it was just a BoundField and not part of a template, they have a DataFormatString property that's used similarly, far as I recall.


  • Closed Accounts Posts: 3,912 ✭✭✭HellFireClub


    You can specify formatting in the databinding expression, eg if you've bound the Text property of your Label in the template;

    <asp:Label ID="lblMyLabel" runat="server" Text='<%# Eval("Price", "{0:C}") %>'/>

    The bolded bit would be the data format string - set to currency in this example. If it was just a BoundField and not part of a template, they have a DataFormatString property that's used similarly, far as I recall.

    Good man this is exactly the solution I was looking for, thanks a mil for that, issue resolved!


  • Registered Users Posts: 515 ✭✭✭NeverSayDie


    Good to hear it worked out. This tutorial has a bit more background if you need it;
    http://www.asp.net/data-access/tutorials/using-templatefields-in-the-detailsview-control-cs
    (bit heavy on the GUI version instead of the markup, but it should give you an overview at least).


Advertisement