Solved

Change font color in gridview loop

Posted on 2007-04-10
8
2,033 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
  • 3
  • 2
8 Comments
 
LVL 27

Expert Comment

by:Sammy
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 earned 500 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now