?
Solved

Change font color in gridview loop

Posted on 2007-04-10
8
Medium Priority
?
2,043 Views
Last Modified: 2013-11-26
I have a gridview in asp.net that has a column displaying "Amount Due" values. I want to loop through the grid and display the values greater than zero in red font. This is what I have attempted, and it doesn't work. I get an 'object not referenced' error. Does anyone have a way to do this?

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim amtDue As Label = DirectCast(gvMyGroups.FindControl("lblAmountDue"), Label)

        For Each amtDue In gvMyGroups.Rows
            If amtDue.Text > 0 Then
                amtDue.ForeColor = Drawing.Color.DarkRed
            End If
        Next

    End Sub
0
Comment
Question by:wdarnellg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 27

Expert Comment

by:Sammy Ageil
ID: 18887143
You can use the GridView's rowdatabound event to change the font
see the event here
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound(vs.80).aspx

Page load will not do anything for you when the page is loading the controls are not built on the page

0
 

Author Comment

by:wdarnellg
ID: 18887237
Thanks sammy, I think I see your point. The truth is however I am having a little trouble understanding how to apply the logic to my problem. How would you use rowdatabound event to change the font color? I'll keep reading. :o)
0
 

Author Comment

by:wdarnellg
ID: 18887491
I feel I am so close. This code gives me a 'can't convert to double' error. Without the ctype I get the red font change, (thanks Sammy) but I only want the values greater than 0 to display in red. What am I missing?

    Protected Sub gvMyGroups_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvMyGroups.RowDataBound

        Dim gvDue As GridViewRow = e.Row

        If gvDue.RowType = DataControlRowType.DataRow Then
            Dim cell As TableCell = e.Row.Cells(7)
            If CType(cell.Text, Double) > 0 Then
                cell.ForeColor = Drawing.Color.DarkRed
            End If
        End If
    End Sub
0
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 
LVL 12

Expert Comment

by:craskin
ID: 18891450
instead of CType(...
just use CInt(cell.Text) > 0

you could be running into trouble with casting nulls as integers or doubles, so in that case, you should nest your test for positive numbers inside an

If Not cell.Text = " " Then

block.
0
 

Author Comment

by:wdarnellg
ID: 18895433
Thank you craskin. I now have something that looks like:

If gvDue.RowType = DataControlRowType.DataRow Then
            Dim cell As TableCell = e.Row.Cells(8)
            If Not CDbl(cell.Text) = " " Then
                If CDbl(cell.Text) > 0 Then
                    cell.ForeColor = Drawing.Color.DarkRed
                End If
            End If
        End If

It makes sense, but the vb.net doesn't want to convert the string (.text) to an integer or double. I have very limited programming experience, and converting data types is a real blind spot in my education. :o)
It may help to know that I am using sqlserver, and the data type of the field we are trying to address is 'smallmoney'.
0
 
LVL 27

Accepted Solution

by:
Sammy Ageil earned 1500 total points
ID: 19035765
use a temp variable to hold the value and use it to check the condition

if gvDue.RowType = DataControlRowType.DataRow Then
            Dim cell As TableCell = e.Row.Cells(8)
            If Not CDbl(cell.Text) = " " Then
             Dim txtVal as Double= double.parse(cell.Text)
                If  txtVal > 0 Then
                    cell.ForeColor = Drawing.Color.DarkRed
                End If
            End If
        End If

Postback if you need any help
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question