简体   繁体   English

更改数值C#的字体颜色

[英]Change the font color of a numeric value C#

trying to change the font color of a numeric value,but getting error:Input string was not a correct format. 尝试更改数值的字体颜色,但收到错误:输入字符串格式不正确。

Eg code: 例如代码:

DataTable dt = PDSData.getPDSBndl(bndlId, 'N');
string qty = "<font color='red'><i><b>" + dt.Rows[i]["qty"].ToString() + "</b></i></font>";
dt.Rows[i]["qty"] =Convert.ToInt32(qty); //@ this line Getting error.

I have tried converting string to int,but no hopes. 我已经尝试将字符串转换为int,但没有希望。

You can not parse something like bgfbgb56ngfngn to int. 你不能解析像bgfbgb56ngfngn这样的东西。 The dataTable itselfs only contains data, no formatting information. dataTable本身只包含数据,没有格式化信息。

You need to change the view (ASP.NET WebForm or razor view, whatever you use) to handle the display. 您需要更改视图(ASP.NET WebForm或razor视图,无论您使用什么)来处理显示。

As you mention in answer of suscha by comment 正如您在评论中回答suscha时提到的那样
"Binding the datatable to a repeater control rpt.DataSource = dt; rpt.DataBind(); – user1676709" “将数据表绑定到转发器控件rpt.DataSource = dt; rpt.DataBind(); - user1676709”
You are using in repeater then you can do like this.. 你在转发器中使用然后你可以这样做..

 <span style="font-weight:bold;font-style:italic;color:#ff0000;"><%# Eval("qty") %></span>

Couple of problems: 几个问题:

  1. Your variable qty will never be an integer as you are adding HTML tags to it. 您的变量qty永远不会是整数,因为您要向其添加HTML标记。
  2. You do not apply font settings on to variables. 您不对字符设置应用于变量。 You do that on the UI element you are using. 您可以在正在使用的UI元素上执行此操作。

The string qty is holding a sting value like '<font color='red'><i><b>" + dt.Rows[i]["qty"].ToString() + "</b></i></font>' & you are trying to cast into int32 which is not valid. 字符串数量保持一个像'<font color='red'><i><b>" + dt.Rows[i]["qty"].ToString() + "</b></i></font>'的刺激值'<font color='red'><i><b>" + dt.Rows[i]["qty"].ToString() + "</b></i></font>'您试图转换为无效的int32。 If qty is holding string value like "9" or "10", it will convert it but in your case it will give error. 如果qty保持字符串值如“9”或“10”,它将转换它但在你的情况下它会给出错误。 Use Int32.TryParse to check whether the string is correct & in out parameter you will get the converted value if tryparse returns true. 使用Int32.TryParse检查字符串是否正确,如果tryparse返回true,则在out参数中将获得转换后的值。

if you want it show in a gridview, manage this in design. 如果您希望它在gridview中显示,请在设计中进行管理。 if you have conditions to set color, then manage in itemdatabound event of grid view. 如果您有条件设置颜色,则在网格视图的itemdatabound事件中进行管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM