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" />
</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" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="TextBox1" Text='<%# Bind("DecimalValue", "0.#####") %>' />
<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
).