Solved

Change font color in gridview loop

Posted on 2007-04-10
8
2,039 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
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

737 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