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!

Gridview Change font color in cell depending on value

Status
Not open for further replies.

Barneye

MIS
Mar 5, 2002
68
0
0
US
Hello!

I have a gridview with phone call statitics for my sales dept. The 6th column is "TotalCalls". I need to change the font color to red if the number of total calls is less than 40 or to green if it is 40 or higher.


I am new to ASP.net, so the simplest explanation will be appreciated.

Thanks!
 
this is one example that i found online, modify it to your requirements...

Code:
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then
   Dim lbl As Label = CType(e.Row.FindControl("lblPreis"), Label)
   If DataBinder.Eval(e.Row.DataItem, "unitprice") < 0D Then
      lbl.ForeColor = Drawing.Color.Red   End If

End If
End Sub

-DNG
 
You need to use the RowDataBound event of the gridview:
Code:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
   If CType(e.Row.Cells(1).Text, Integer) < 40 Then
      e.Row.Cells(1).ForeColor = Drawing.Color.Red
    Else
       e.Row.Cells(1).ForeColor = Drawing.Color.Green
   End If
End If
Let me know if you need further explination,

Jim


End Sub
[/code]
 
Jim,

Right on target!

If you have time, a short explination of the code would be helpfull so I understand it for the future.


Thanks!
 
The RowDataBound event as each row binds to the datasource. So, this is a good place to check data and do anything that is dependent on it

The first line check the Row type. If you look in intellisense will show you the differnt types. Basically it can be a header, footer, datarow..etc.

The second line converts the text in the cell to an integer because all values in the cells are text. The cell index is an integer, zero based(from 0 to number of columns -1). So column(0) is the first column.

The rest is prety self explanitory, check the value after the conversion based on your specs, and change the text (forecolor) as you need.

Jim

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top