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}
This is the formula I used for the data format {0:D}
http://www.velocityreviews.com/forums/t117866-gridview-decimal-point-is-a-comma.html
use
try to use {0:f4} it will works
use
try to use {0:f4} it will works
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.
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.
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
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
ASKER
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
ASKER
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.
ASKER
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False "
DataSourceID="SqlDataSourc e1" 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="Descriptio n" />
<asp:BoundField DataField="Acount_Num" HeaderText="Acount_Num" SortExpression="Acount_Num " />
<asp:BoundField DataField="DueDate" HeaderText="DueDate" SortExpression="DueDate" />
</Columns>
</asp:GridView>
DataSourceID="SqlDataSourc
<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="Descriptio
<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.
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.
ASKER
applyformatineditmode="tru e"
ok I did that and still same result
ok I did that and still same result
ASKER
<asp:BoundField DataField="Rate" DataFormatString="{0:p}" HtmlEncode="false" HeaderText="Rate" SortExpression="Rate" />
Found the answer HtmlEncode="false" but it cannot have applyformatineditmode="tru e" included or else it wont work
Found the answer HtmlEncode="false" but it cannot have applyformatineditmode="tru
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://msdn2.microsoft.com/en-us/library/dwhawy9k.aspx