Search code examples
asp.netaspxgridview

How to add DataFormatString in a TemplateField in aspx page?


I am working with a Gridview in aspx page. To be more specific, in a gridview, I'm working on a specific column called Price

The Price column value examples in the gridview should look something like this:

 Price
 1.50 AUD
10.00 AUD
15.55 AUD
100.50 AUD

In order to achieve this result, this is what I have tried in the aspx file:

<asp:TemplateField HeaderText="Price" DataFormatString="{0} AUD" >
         <ItemTemplate>
          <asp:TextBox ID ="TextBox1" runat="server" DataField="Product_Price" Text='<%#Eval("Price")%>' />
          <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Numbers with decimals only" ControlToValidate="TextBox1" ValidationExpression="^[0-9]+(\.[0-9][0-9]?)?"></asp:RegularExpressionValidator>
          <asp:Button ID ="Button1" runat="server" OnClick="Update_Click" CommandArgument="Button7" CommandName="Update"  Text="Update" />
          </ItemTemplate>
          </asp:TemplateField>

When I bring the mouse cursor on DataFormatString syntax, this is the error message being shown:

Validation(ASP.NET): Attribute 'DataFormatString' is not a valid attribute of element 'TemplateField'

Where have I made the mistake ?

It would be helpful if the appropriate solution syntax is provided.


Solution

  • Try

    <asp:TextBox ID ="TextBox1" runat="server" DataField="Product_Price" Text='<%#Eval("Price","{0:c}")%>' />