Databinding Error when Recreating object
Figure there is a simple solution to this problem but I have been unable to find it.
I have databinding in an ASP.Net application to a GridView. This gridview is bound to an ObjectDataSource as per standard usage.
The problem I have is that one of my bound fields uses the property DataFormatString="{0:C}" and due to the currency format being displayed when an update is attempted and the object recreated I get a error as such "$13.00 is not a valid value for Decimal."
Clearly this is a result of the column using a FormatString and then attempting to bind it back to a decimal property I have in my object called UnitPrice.
I am assuming there is some markup I can set that can specify how the value is translated back?
Thanks in advance for any help.
For anyone curious the solution ended up looking like this...
<asp:TemplateField>
<HeaderTemplate>
UnitPrice
</HeaderTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditItem" runat="server" Text='<%# Bind("UnitPrice", "{0:#,##0.00}") %>' Enabled="false" ></asp:Label>
</EditItemTemplate>
<ItemTemplate>
&开发者_Go百科lt;asp:Label Runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>' ID="lblUnitPrice"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Do not include the format string in the EditItemTemplate. Just bind the raw value.
Something like this:
<asp:TemplateField SortExpression="UnitPrice" HeaderText="Unit Price">
<EditItemTemplate>
<asp:TextBox ID="editUnitPrice" Runat="server" Text='<%# Bind("UnitPrice", "{0:#,##0.00}") %>' ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>' ID="Label1"> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
精彩评论