简体   繁体   中英

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.

You can not parse something like bgfbgb56ngfngn to int. The dataTable itselfs only contains data, no formatting information.

You need to change the view (ASP.NET WebForm or razor view, whatever you use) to handle the display.

As you mention in answer of suscha by comment
"Binding the datatable to a repeater control 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.
  2. You do not apply font settings on to variables. You do that on the UI element you are using.

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. If qty is holding string value like "9" or "10", it will convert it but in your case it will give error. Use Int32.TryParse to check whether the string is correct & in out parameter you will get the converted value if tryparse returns true.

if you want it show in a gridview, manage this in design. if you have conditions to set color, then manage in itemdatabound event of grid view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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