2014-03-31 34 views
0

我有一个模板字段,如下面的GridView控件所示。输入字符串与GridView中的TemplateField格式不正确

<asp:TemplateField HeaderText="Total" SortExpression="CYTotal">    
      <ItemTemplate> 
       <asp:Label ID="lblCYTotal" runat="server" MaxLength="20" Width="30" ReadOnly ="true" 
       Text = '<%# Bind("CYTotal","{0:#,##0}") %>' Font-Names="Tahoma" Font-Size="8pt"> </asp:Label> 
      </ItemTemplate>   
       <HeaderStyle Width="30px" Font-Names="Tahoma" Font-Size="8pt"/> 
       <ItemStyle Width="30px" HorizontalAlign="Right" />  
      </asp:TemplateField> 

我设置在RowDataBound事件模板字段内标签的格式如下图所示

if (accountTextBox.Text != "Gross Margin") 
       {     
        lblCYTotal.Text = String.Format("{0:$#,##0}", Convert.ToDecimal(lblCYTotal.Text)); 
} 
else if (accountTextBox.Text == "Gross Margin") 
       {     
        lblCYTotal.Text = String.Format("{0:P0}", Convert.ToDecimal(lblCYTotal.Text)); 
} 

,然后在RowUpdating事件中,我使用下面的代码,但它是给我的错误“*输入字符串不正确forma *吨”。 我试着用下面的“替换”去除“%”符号,但没有运气。我也尝试使用NumberStyles.Currency和NumberStyles.Float。

decimal currency = decimal.Parse(lblCYTotal.Text.Replace("%",""), NumberStyles.Any); 

任何帮助将不胜感激!

+0

为什么你不取代$?因为从你的代码看,它可能在你的文本框中可能为$。如果是的话,它肯定会给你一个例外。 –

+0

感谢Manish,我应该提到我曾尝试更换$和%但仍然得到相同的错误。 – sam

+0

好吧,但是你没有提到你在哪一行出错,以及在出错时你的文本框是什么。 –

回答

0

好的我找到了问题。从数据库中提取的其中一行具有NULL值。我使它显示为零(使用ISNULL函数)并且错误消失了。我不确定这是否是最好的方法,但我找不出任何其他方式。

我在后面的代码中有if语句来检查NULLS并只格式化非空值,但它不起作用。