EnjoySharePoint YouTube Channel

Apply color in gridview rows based dynamically

This is an article about filling colors in gridview based on some conditions and values.
Say for an example: if a gridview has different departments in different locations. Need to color the gridview values based on the locations and display the top scorers of the winner’s department wise. As given below :
Add  OnRowDataBound EVENT for the gridview and the find
protected void GvLeaderBorad_OnRowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
  Label lblNett = e.Row.FindControl(“lbl_Net”) as Label;
                if (lblNett.Text == Locations.Chennai.ToString())
                    e.Row.Cells[0].BackColor = System.Drawing.Color.DarkGreen;
When the color code is fetched from label placed inside the row . It will be applied to the entire row.
Thus in code itself when we bind the grid. Provide row color in the label and fetch the color code from it.
Apply the color code(Html hex code ex: #fffff ) to the gridview row.
Hope this article will teach you how to add colors in gridview rows.
Check out Best Alternative to InfoPath -> Try Now
You May Also like the Following SharePoint Tutorials:

About jayabharathi

The turning point in the process of growing up is when you discover the core of strength within you that survives all hurt.

View all posts by jayabharathi →