Solved

Change font color in gridview loop

Posted on 2007-04-10
8
2,036 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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