Conditional formatting - Gridview cell
I’m using Visual Studio 2007 and I’m using a grid view control to display data. One of the fields in the gridview control displays numeric data. If the numeric data is less than zero, then I want the backcolor to be red.
1. I have changed the field that displays the data to a template field.
2. I changed the name of the label in the template field from Label1 to lblAmount
In the databinding event of the grid control I placed the following code
Code:
Dim lblAmount As Label = Me.GridView1.FindControl("lblAmount")
If lblAmount.Text < 0 Then
lblAmount.BackColor = Color.Red
End If
When I run this code I get an error Object reference not set to an instance of an object.
I have tried this code in the databinding and databound event of my gridview control and I get the same error. How can I accomplish what I want – making the label red?
Thanks
GEM
Re: Conditional formatting - Gridview cell
The label is present within the Row of the GridView and not the Gridview itself.
In the RowDataBouund of GridView,
Code:
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lblAmount As Label = e.Row.FindControl("lblAmount")
If lblAmount.Text = "0" Then
e.Row.BackColor = Drawing.Color.Red
End If
End If
Hope it helps, just typed from top of my head!!! :bigyello:
Re: Conditional formatting - Gridview cell
Thanks,
I'll try that when I get to work.....Just off the top of your head, where should this go, in the databinding or databound event or somewhere else.
Thanks
GEM
Re: Conditional formatting - Gridview cell