|
-
Jul 31st, 2007, 05:08 AM
#1
Thread Starter
Addicted Member
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
-
Jul 31st, 2007, 05:30 AM
#2
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!!!
-
Jul 31st, 2007, 05:40 AM
#3
Thread Starter
Addicted Member
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
-
Jul 31st, 2007, 05:57 AM
#4
Re: Conditional formatting - Gridview cell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|