Results 1 to 8 of 8

Thread: Color Coding Rows (Not working)

  1. #1
    New Member
    Join Date
    Aug 12
    Posts
    4

    Question 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

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,464

    Re: Color Coding Rows (Not working)

    What event are we in here?
    Last edited by dunfiddlin; Aug 14th, 2012 at 01:37 PM.

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,182

    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.

  4. #4
    New Member
    Join Date
    Aug 12
    Posts
    4

    Re: Color Coding Rows (Not working)

    Quote Originally Posted by dunfiddlin View Post
    What event are we in here?
    The person below you mentioned it. Its GridView, sorry for not specifying it in the OP.

  5. #5
    New Member
    Join Date
    Aug 12
    Posts
    4

    Re: Color Coding Rows (Not working)

    Quote Originally Posted by MattP View Post
    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!

  6. #6
    New Member
    Join Date
    Aug 12
    Posts
    4

    Re: Color Coding Rows (Not working)

    Quote Originally Posted by MattP View Post
    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? =/

  7. #7
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,182

    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.

  8. #8
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 04
    Location
    The Granite City
    Posts
    21,729

    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
  •