2010-04-26 34 views
0

圖中存在對此問題的簡單解決方案,但我一直無法找到它。重新創建對象時的數據綁定錯誤

我有一個ASP.Net應用程序到一個GridView的數據綁定。按照標準用法,這個gridview綁定到一個ObjectDataSource。

我遇到的問題是我的一個綁定字段使用屬性DataFormatString =「{0:C}」,並且由於嘗試更新並重新創建對象時顯示的貨幣格式,因此我得到一個錯誤「$ 13.00不是Decimal的有效值。」

很明顯,這是使用FormatString的列的結果,然後試圖將其綁定回到我的對象中名爲UnitPrice的十進制屬性。

我假設有一些標記可以設置,可以指定如何將值轉換回來?

在此先感謝您的幫助。

對於任何人好奇的解決方案最終看上去像這樣...

<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

不要在EditItemTemplate模板的格式字符串。只要綁定原始值。

事情是這樣的:

<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> 
+0

非常感謝隊友,你在單價實際上是一個只讀列,但之後我改變了代碼,你提供的比特使用標記物的結合方面有點過工作得很好... 再次感謝。 – 2010-04-27 04:21:09

+0

不用擔心。祝你的項目好運。 – 2010-04-28 19:09:44