• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5997
  • Last Modified:

How to format a decimal data type (SQL Server 2000) when displayed (in datagrid)

I have field that is decimal datatype (10,4).  Currently, when a number such as 3.25 is entered, it shows up as 3.2500 (after it is saved to the db and retrieved again).  And whole numbers, such as 44, show up as 44.0000.  I would like to have have the zeros show up at the right of the numbers.  Is there a format that will do this?  So, for 44, I really do want just 44 to show up.  And for 3.25, I really want just 3.25 to show up.
Thank you!
0
sah18
Asked:
sah18
  • 4
  • 3
  • 3
1 Solution
 
sah18Author Commented:
Here is the column in my datagrid that is displaying this number:

<asp:TemplateColumn HeaderText="Dose">
      <ItemTemplate>
            <%# DataBinder.Eval(Container.DataItem, "dosemg") %>
      </ItemTemplate>
      <EditItemTemplate>
            <asp:TextBox Width="50px" ID="dosemg" Runat=server Text='<%# DataBinder.Eval(Container.DataItem, "dosemg") %>' />
            <asp:requiredfieldvalidator id="RequiredfieldvalidatorDosemg" runat="server" Font-Size="XX-Small" Font-Names="Webdings"
                  Display="Dynamic" ErrorMessage="=" ControlToValidate="dosemg" />
      </EditItemTemplate>
</asp:TemplateColumn>

So, I guess I need to somehow modify
<%# DataBinder.Eval(Container.DataItem, "dosemg") %>
to have the format I want in both the ItemTemplate and also the EditItemTemplate.

Any suggestions??

Thanks for your help!
0
 
GavinMannionCommented:
You will want to use the DataFormatString option the DataGrid...

Try.... dataformatstring="{0:D2}"
0
 
sah18Author Commented:
I'm just not seeing this as an option.  I only see it as an option for the BoundColumns.  Where should I place this setting?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
tusharashahCommented:
Since you are using template column, Modify DataBinder.Eval like following:

  <%# DataBinder.Eval(Container.DataItem, "dosemg", "{0:D2}") %>

-tushar
0
 
GavinMannionCommented:
I didn't see your second post until after mine had submitted....

On a textbox I would only know how to do it using the OnItemDataBound event.

There you can access the textbox value and change it as required.

Hopefully someone else knows an easier way?
0
 
GavinMannionCommented:
Hah there you go :)
0
 
tusharashahCommented:
** you'll need to modify like following.. D2 works only with integer..

<%# DataBinder.Eval(Container.DataItem, "dosemg", "{0:N2}") %>

here's other list: http://msdn2.microsoft.com/en-US/library/31723w77(VS.80).aspx
0
 
sah18Author Commented:
tusharashah - this is getting closer to what I want, but isn't quite it!
now, i see 35.00 when I want to see 35
is there a way for it not to buffer with the trailing zeros at all?
and I don't want to truncate what has been entered at all, just display it without any extra zeros on the right.
so:  123.4567, I would still want to be displayed as 123.4567
but: 123.9000, I would want to be displayed as 123.9
do you know what i mean?
thank you again for your time!!
0
 
tusharashahCommented:
The format function will work similarly for every row, if you need certain change you can write down your own function and call it like following..

Call in your ASPX:
   <%# FormatNumber(DataBinder.Eval(Container.DataItem, "dosemg") %>

Public Function in code behind:
      public string FormatNumber(string strNumber)
      {
            if( strNumber.IndexOf(".") > -1 ) strNumber = strNumber.TrimEnd('0');
            if( strNumber.IndexOf(".") == strNumber.Length-1 ) strNumber = strNumber.Remove( strNumber.Length-1, 1);
            return strNumber;
      }

-tushar
0
 
sah18Author Commented:
Thank you so much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now