How to change forecolor of a label inside a Gridview according to the Database value
In this article I am going to explain how we can change the forecolor of a label inside the gridview without any code behind scripts.
The same code can also be used in changing the checkbox status according to the values in database.
Our Gridview will become more responsive and beautiful when we change its style according to the values it displays to the user. For example the fore color and back color of labels, linkbuttons etc. We can do the above styling from code behind. But this takes much more execution time, hence becoming a menace to the users. Through the below code snippet you can just do these styling stuff without any code behind script which in turn makes it a faster in execution So here I am going to display these user table details at the front end by using a Gridview. In that I use a Label to display the approval status that is whether Approved or Disapproved.
Here I used the following table.
My front end code looks like this.
Inside SCRIPT tag add this.
protected string mysubProgram(string str)
{
if (str == "A")
{
return "Approved";
}
else if (str == "D")
{
return "Rejected";
}
else
{
return "Pending";
}
}
protected System.Drawing.Color mysubProgram1(string str)
{
if (str == "A")
{
return System.Drawing.Color.Green;
}
else if (str == "D")
{
return System.Drawing.Color.Red;
}
else
{
return System.Drawing.Color.Black;
}
}
Inside your gridview add this.
OUTPUT
The same method can be also used for setting the checkbox checked or unchecked in Gridview.
Hope that this article helps you all.