views:

13

answers:

1

Hi All,

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>
         <asp:Label Runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>' ID="lblUnitPrice"></asp:Label>
     </ItemTemplate>
</asp:TemplateField>
+1  A: 

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>
Daniel Dyson
Thanks so much mate, you were a little off in terms of the unitprice actually being a readonly column but after I changed the code you provided a bit to use a label the binding worked fine...Thanks again.
Maxim Gershkovich
No worries. Good luck with your project.
Daniel Dyson