Link to home
Start Free TrialLog in
Avatar of Scripter25
Scripter25

asked on

GridView Decimal Format

I have a GridView with a decimal value in it This decimal value can be up to 4 digits to the right of decimal point but how do I get it to display only two digits if the far right 2 are 0's  in other words say I have a decimal that is .21 and one that is .212  how I currently have the Gridview setup it will display the values as .2100 and .2120

This is the formula I used for the data format {0:D}
Avatar of dstanley9
dstanley9

use {0:N4}.   D is for integral types:

http://msdn2.microsoft.com/en-us/library/dwhawy9k.aspx
http://msconline.maconstate.edu/tutorials/ASPNET2/ASPNET07/aspnet07-01.aspx

Format String Description
{0:Cn} Currency. Displays numeric values in currency format with a leading dollar sign; n indicates the number of decimal places. If n is omitted, the default currency precision is two decimal digits.
{0:Dn} Decimal. Displays integer values; n indicates the minimum number of digits desired in the resulting string. If necessary, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.
{0:Fn} Fixed-point. Displays numeric values in fixed format; n indicates the desired number of decimal places.
{0:Nn} Number. The number is converted to a string; n indicates the desired number of decimal places. Commas are inserted between each group of three digits to the left of the decimal point.
{0:Pn} Percent. Displays numeric values in percentage format; n indicates the desired number of decimal places.
{0} Text. Displays a text string.
Avatar of Scripter25

ASKER

Ok Team I have tried it each way you have suggested and ways that I found on the sites that you have sent me. Now I know that one of these ways should have worked but something is going wrong.
It is not changing format at all. No matter what I do even if I try to force feed a % sign into it.
I am not hard coding it into the page but instead using the Fields editor to change the DataFormatString property
The Data Type in the database is  decimal(18, 4)   should I try to go with something else? my overall goal of this project is making a financial calculator for my sister so I figured seeing as how a database does not have a percentage type I could always use the decimal(18, 4) type
I have changed the DB data type to float with same result. also just fyi maybe this has something to do with it. I have the Gridview in an Updatepanel
Can you post the html for your GridView (everything between the <asp:gridview> tags?  Something else may be going on.
               <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                    DataSourceID="SqlDataSource1" Style="position: static">
                    <Columns>
                        <asp:BoundField DataField="INT" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                            SortExpression="INT" />
                        <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
                        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                        <asp:BoundField DataField="Balance" HeaderText="Balance" SortExpression="Balance" />
                        <asp:BoundField DataField="Available" HeaderText="Available" SortExpression="Available" />
                        <asp:BoundField DataField="AmountDue" HeaderText="AmountDue" SortExpression="AmountDue" />
                        <asp:BoundField DataField="Rate" DataFormatString="{0:P2}" HeaderText="Rate" SortExpression="Rate" />
                        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                        <asp:BoundField DataField="Acount_Num" HeaderText="Acount_Num" SortExpression="Acount_Num" />
                        <asp:BoundField DataField="DueDate" HeaderText="DueDate" SortExpression="DueDate" />
                    </Columns>
                </asp:GridView>
Are you looking at it in edit mode?  (from MSDN docs at http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx):

By default, the formatting string is applied to the field value only when the data-bound control that contains the BoundField object is in read-only mode. To apply the formatting string to field values while in edit mode, set the ApplyFormatInEditMode property to true.
applyformatineditmode="true"

ok I did that and still same result
                       <asp:BoundField DataField="Rate" DataFormatString="{0:p}" HtmlEncode="false" HeaderText="Rate" SortExpression="Rate" />


Found the answer HtmlEncode="false"  but it cannot have applyformatineditmode="true" included or else it wont work
ASKER CERTIFIED SOLUTION
Avatar of namansshah
namansshah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial