Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I carry out Conditional formating on a Gridview ?

Controls

How can I carry out Conditional formating on a Gridview ?

by  Chance1234  Posted    (Edited  )
Say in your gridview you have the following data ;

Month, Target, Result
June, 0.23, 0.24
July, 0.17, 0.15
Sept, 0.25, 0.37

In your Gridview, you want to highlight the cell when the Months result is less than the Target.

What you want to do is the following.

On the RowDataBound event of the gridview add the following code.

Code:
//First check to see that we are on a data row 
  if (e.Row.RowType == DataControlRowType.DataRow)
            {
//now convert the text value to a double (use int32 for whole numbers)
                if (Double.Parse(e.Row.Cells[2].Text) < Double.Parse(e.Row.Cells[1].Text))
                {
//set the background to red
                    e.Row.Cells[5].Style.Add("background-color", "red");
//you can also access other properties such as font from here, as below
                    e.Row.Cells[5].Font.Bold = true;

                }
               
            }
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top