|
-
Aug 14th, 2012, 01:21 PM
#1
Thread Starter
New Member
Color Coding Rows (Not working)
Below is the source code that tries to highlight rows in certain colors depending on value of the certificate. The code runs, but the colors are not showing up. Am i missing something here? Any help would be appreciated. Kudos!
If e.Row.RowType = DataControlRowType.DataRow Then
If CType(sender, GridView).DataKeys(e.Row.RowIndex).Values("Certificate") <100000 Then
e.Row.BackColor = System.Drawing.Color.lightblue
elseIf CType(sender, GridView).DataKeys(e.Row.RowIndex).Values("Certificate") < 1000000 And ("Certificate") > 100000 Then
e.Row.BackColor = System.Drawing.Color.lightGreen
elseIf CType(sender, GridView).DataKeys(e.Row.RowIndex).Values("Certificate") < 10000000 And ("Certificate") > 1000000 Then
e.Row.BackColor = System.Drawing.Color.lightyellow
else
e.Row.BackColor = System.Drawing.Color.blue
End if
End If
End Sub
-
Aug 14th, 2012, 01:27 PM
#2
Re: Color Coding Rows (Not working)
What event are we in here?
Last edited by dunfiddlin; Aug 14th, 2012 at 01:37 PM.
-
Aug 14th, 2012, 02:12 PM
#3
Re: Color Coding Rows (Not working)
I'm going to assume this is a GridView or DetailsView in ASP.NET.
You're running into a problem with "...And ("Certificate")...". Rather than the clunky CType(sender, GridView).DataKeys(e.Row.RowIndex).Values("Certificate") that you're using all over the place it would be better to Dim a local variable.
Code:
Protected Sub TestGridView_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles TestGridView.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cert = CType(sender, GridView).DataKeys(e.Row.RowIndex).Values("Certificate")
If cert < 100000 Then
e.Row.BackColor = Color.LightBlue
ElseIf cert < 1000000 AndAlso cert > 100000 Then
e.Row.BackColor = Color.LightGreen
ElseIf cert < 10000000 AndAlso cert > 1000000 Then
e.Row.BackColor = Color.LightYellow
Else
e.Row.BackColor = Color.Blue
End If
End If
End Sub
Another way of looking at it.
Code:
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cert As Long = CLng(DataBinder.Eval(e.Row.DataItem, "Certificate"))
Select Case cert
Case cert < 100000
e.Row.BackColor = Color.LightBlue
Case cert < 1000000 AndAlso cert > 100000
e.Row.BackColor = Color.LightGreen
Case cert < 10000000 AndAlso cert > 1000000
e.Row.BackColor = Color.LightYellow
Case Else
e.Row.BackColor = Color.Blue
End Select
End If
Last edited by MattP; Aug 14th, 2012 at 02:17 PM.
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
Aug 14th, 2012, 05:51 PM
#4
Thread Starter
New Member
Re: Color Coding Rows (Not working)
 Originally Posted by dunfiddlin
What event are we in here?
The person below you mentioned it. Its GridView, sorry for not specifying it in the OP.
-
Aug 14th, 2012, 05:58 PM
#5
Thread Starter
New Member
Re: Color Coding Rows (Not working)
 Originally Posted by MattP
I'm going to assume this is a GridView or DetailsView in ASP.NET.
You're running into a problem with "...And ("Certificate")...". Rather than the clunky CType(sender, GridView).DataKeys(e.Row.RowIndex).Values("Certificate") that you're using all over the place it would be better to Dim a local variable.
Code:
Protected Sub TestGridView_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles TestGridView.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cert = CType(sender, GridView).DataKeys(e.Row.RowIndex).Values("Certificate")
If cert < 100000 Then
e.Row.BackColor = Color.LightBlue
ElseIf cert < 1000000 AndAlso cert > 100000 Then
e.Row.BackColor = Color.LightGreen
ElseIf cert < 10000000 AndAlso cert > 1000000 Then
e.Row.BackColor = Color.LightYellow
Else
e.Row.BackColor = Color.Blue
End If
End If
End Sub
Another way of looking at it.
Code:
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cert As Long = CLng(DataBinder.Eval(e.Row.DataItem, "Certificate"))
Select Case cert
Case cert < 100000
e.Row.BackColor = Color.LightBlue
Case cert < 1000000 AndAlso cert > 100000
e.Row.BackColor = Color.LightGreen
Case cert < 10000000 AndAlso cert > 1000000
e.Row.BackColor = Color.LightYellow
Case Else
e.Row.BackColor = Color.Blue
End Select
End If
Thanks for the help! I will try this and let you know how it goes!
-
Aug 15th, 2012, 09:54 AM
#6
Thread Starter
New Member
Re: Color Coding Rows (Not working)
 Originally Posted by MattP
I'm going to assume this is a GridView or DetailsView in ASP.NET.
You're running into a problem with "...And ("Certificate")...". Rather than the clunky CType(sender, GridView).DataKeys(e.Row.RowIndex).Values("Certificate") that you're using all over the place it would be better to Dim a local variable.
Code:
Protected Sub TestGridView_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles TestGridView.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cert = CType(sender, GridView).DataKeys(e.Row.RowIndex).Values("Certificate")
If cert < 100000 Then
e.Row.BackColor = Color.LightBlue
ElseIf cert < 1000000 AndAlso cert > 100000 Then
e.Row.BackColor = Color.LightGreen
ElseIf cert < 10000000 AndAlso cert > 1000000 Then
e.Row.BackColor = Color.LightYellow
Else
e.Row.BackColor = Color.Blue
End If
End If
End Sub
Another way of looking at it.
Code:
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cert As Long = CLng(DataBinder.Eval(e.Row.DataItem, "Certificate"))
Select Case cert
Case cert < 100000
e.Row.BackColor = Color.LightBlue
Case cert < 1000000 AndAlso cert > 100000
e.Row.BackColor = Color.LightGreen
Case cert < 10000000 AndAlso cert > 1000000
e.Row.BackColor = Color.LightYellow
Case Else
e.Row.BackColor = Color.Blue
End Select
End If
Tried both methods but no luck, here is the original source code with the event included:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Label1.Text = "Awaiting Initial Screen (n=" & Me.GridView1.Rows.Count & ")" _
& "<br />[" & Now().ToString("g") & "; refresh=15min]"
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If CType(sender, GridView).DataKeys(e.Row.RowIndex).Values("Certificate") <100000 Then
e.Row.BackColor = System.Drawing.Color.lightblue
elseIf CType(sender, GridView).DataKeys(e.Row.RowIndex).Values("Certificate") < 1000000
e.Row.BackColor = System.Drawing.Color.lightGreen
elseIf CType(sender, GridView).DataKeys(e.Row.RowIndex).Values("Certificate") < 10000000
e.Row.BackColor = System.Drawing.Color.lightyellow
else
e.Row.BackColor = System.Drawing.Color.blue
End if
End If
End Sub
Any reason why so far none of these codes have worked? =/
-
Aug 16th, 2012, 09:00 AM
#7
Re: Color Coding Rows (Not working)
No reason I can see that they haven't worked other than you're not comparing the value you think you are. Dim a local variable as I showed in the examples above and put in a breakpoint. See if the value in that variable is what you expect it to be, then step through the If Then statements to see if you hit the expected result.
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
Aug 18th, 2012, 01:44 PM
#8
Re: Color Coding Rows (Not working)
Moved to the ASP.NET Forum.
Gary
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
|