开发者

decimal values in a gridview

开发者 https://www.devze.com 2023-01-14 11:31 出处:网络
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 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 de开发者_运维问答cimal point. Like 0.12345 is acceptable and 0.123456 is not acceptable.

How to restrict this..

Plz help


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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消