Solved

Change font color in gridview loop

Posted on 2007-04-10
8
2,034 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 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

21 Experts available now in Live!

Get 1:1 Help Now