?
Solved

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

Posted on 2006-04-21
10
Medium Priority
?
5,993 Views
Last Modified: 2008-03-04
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
Comment
Question by:sah18
  • 4
  • 3
  • 3
10 Comments
 
LVL 9

Author Comment

by:sah18
ID: 16506612
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
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16506649
You will want to use the DataFormatString option the DataGrid...

Try.... dataformatstring="{0:D2}"
0
 
LVL 9

Author Comment

by:sah18
ID: 16507206
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 18

Expert Comment

by:tusharashah
ID: 16507332
Since you are using template column, Modify DataBinder.Eval like following:

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

-tushar
0
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16507338
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
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16507345
Hah there you go :)
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 16507352
** 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
 
LVL 9

Author Comment

by:sah18
ID: 16507446
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
 
LVL 18

Accepted Solution

by:
tusharashah earned 1200 total points
ID: 16507805
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
 
LVL 9

Author Comment

by:sah18
ID: 16507832
Thank you so much!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

807 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