• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 623
  • Last Modified:

Add Color To List Item Dynamically C#

In the code below I am attempting to add a color to the text coming into the list item. I maybe writing this totally wrong. All I wanna do is depending on the "ProductId" I wanna change the color of the text in "Name".
For productid=2 want "Name" text to be bronze
For productid=3 want "Name" text to be silver
For productid=4 want "Name" text to be gold

for (int i = 0; i < dt.Rows.Count; i++)
            // here 'images' corresponds to the folder in your application
            string imgsrc = "images/" + dt.Rows[i]["imageFileName"].ToString(); ;
            string value = dt.Rows[i]["ProductId"].ToString();
            ListItem item=new ListItem(String.Format("<img src=\"{0}\">" +  " "+dt.Rows[i]["Name"].ToString(), imgsrc), value);
             switch (value.ToString())
                case "2":
                    item.Attributes.CssStyle = 
                case "3":
                    list.ForeColor = System.Drawing.Color.Silver;
                case "4":
                    list.ForeColor = System.Drawing.Color.Gold;
            //list.Items.Add(new ListItem(String.Format("<img src=\"{0}\">" +  " "+dt.Rows[i]["Name"].ToString(), imgsrc), value));

Open in new window

1 Solution
Try setting CSS attribute like  "Attributes.CssStyle.Add("color", "red")".
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now