views:

19

answers:

2

How to display a decimal value in the gridview. Say if user enters a value 0.5, it got inserted into DB as 0.5, but while diplaying it back on the the front end , it is displayed as 0.50.

How to avoid this scenario.

And also the user should be able to enter 5 places after the decimal point. Like 0.12345 is acceptable and 0.123456 is not acceptable.

How to restrict this..

Plz help

A: 

Apply client-side javascript for formatting the value to the desired scale and precision.

Dienekes
A: 

You can do string formatting inside an Eval statement using String.Format-style rules when you're databinding:

<asp:TemplateField HeaderText="Decimal">
    <ItemTemplate>
        <asp:Label ID="Label1" Text='<%# Eval("DecimalValue", "{0:0.#####}") %>' runat="server"  />&nbsp;
    </ItemTemplate>
</asp:TemplateField>

This label will have up to five digits after the decimal point, so you'll see 0.1, 0.01, 0.001 etc but not 0.1000. See Custom numeric format strings.

For data entry, I'd probably use the AjaxControlToolkit to put a mask on a TextBox:

<asp:TemplateField HeaderText="Decimal">
    <ItemTemplate>
        <asp:Label ID="Label1" Text='<%# Eval("DecimalValue", "{0:0.#####}") %>' runat="server"  />&nbsp;
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox runat="server" ID="TextBox1" Text='<%# Bind("DecimalValue", "0.#####") %>' />&nbsp;
        <ajaxtoolkit:MaskedEditExtender runat="server" TargetControlID="TextBox1" MaskType="Number" Mask="99999.99999" />
    </EditItemTemplate>
</asp:TemplateField>

The masked edit extender attached to the textbox will only accept numbers (MaskType="Number"), fills in empty mask characters with zeroes (also from the mask type) and will accept up to six digits either side of the decimal point (Mask="999999.99999).

PhilPursglove
Text='<% #Eval("Threshold_Min","{0:0.#####}") %>'Could you please explain me in details actually the text box should accept the values between 0 and 999999.The Values may be between 0.00000 to 999999.99999.Please help
K Ratnajyothi