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



Here I used the following table.

REQUIRED USER TABLE

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.




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.


Gridview tag


OUTPUT

OUTPUT GRID


The same method can be also used for setting the checkbox checked or unchecked in Gridview.

Hope that this article helps you all.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: