Results 1 to 8 of 8

Thread: Color Coding Rows (Not working)

Threaded View

  1. #4
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width