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
Re: Color Coding Rows (Not working)
What event are we in here?
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
Re: Color Coding Rows (Not working)
Quote:
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.
Re: Color Coding Rows (Not working)
Quote:
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!
Re: Color Coding Rows (Not working)
Quote:
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? =/
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.
Re: Color Coding Rows (Not working)
Moved to the ASP.NET Forum.
Gary