Solved

Change the font color of a label in a gridview (That is bind to a field) when the amount is negative

Posted on 2007-04-05
5
1,108 Views
Last Modified: 2008-01-09
ASP.NET, VB.NET, .NET 2.0

How can I make the text of a column in a GridView Red if the amount is negative?

I have a binded field in a grid view, and need to make it red if the amount is less than zero.  How can I do this?  

Here is my code:
<ItemTemplate>
                    <asp:Label ID="Label12" runat="server" Text='<%# Bind("AvailBudget") %>' Width="100px"></asp:Label>
                </ItemTemplate>

If the AvailBudget field is negative, I want to change the font color on the label to red
0
Comment
Question by:Epicmanagement
  • 3
5 Comments
 
LVL 27

Expert Comment

by:VBRocks
Comment Utility
You can use this in a Windows form:

    Private Sub DataGridView1_CellLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
        If Me.DataGridView1.CurrentCell.Value < 0 Then
            Me.DataGridView1.CurrentCell.Style.ForeColor = Color.Red

        End If
    End Sub
0
 
LVL 12

Accepted Solution

by:
craskin earned 500 total points
Comment Utility
in a subroutine that runs on the GridViewDataBound event you can do:

For Each row as GridViewRow in myGridView.Rows
    If CInt(row.Cells(the cell number of your template starting from 0).Text) < 0 Then
         row.BackColor = Drawing.Color.Red
    End If
Next


you can even put this immediately after the myGridView.DataBind() line if you're programmatically databinding. if you're using a sqlobject, then you have to use the OnDataBound event.
0
 
LVL 27

Expert Comment

by:VBRocks
Comment Utility
You might check for the column name as well, since you only want it applied to AvailBudget field

   Private Sub DataGridView1_CellLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
        If Me.DataGridView1.CurrentCell.OwningColumn.Name.Contains("AvailBudget ") AndAlso _
                Me.DataGridView1.CurrentCell.Value < 0 Then

            Me.DataGridView1.CurrentCell.Style.ForeColor = Color.Red

        End If
    End Sub
0
 

Author Comment

by:Epicmanagement
Comment Utility
None of the above work for me.  The only that is close is craskin: post.

But they sytnax is wrong since my field is a string and not an integer

I need something like .toString.Contains("-") then

I only want either the text of the label red, or just that one column red, the whole row doesn't need to be red.  craskin post makes the whole row red, but the code isn't working correctly becase my value is not an Integer it is just a string.  I guess I need something like
0
 
LVL 27

Expert Comment

by:VBRocks
Comment Utility
You can try this on the DataGridView.Cell formatting event:

Private Sub dataGridView1_CellFormatting(ByVal sender As Object, _
    ByVal e As DataGridViewCellFormattingEventArgs) _
    Handles dataGridView1.CellFormatting

    If Me.dataGridView1.Columns(e.ColumnIndex).Name.Contains("AvailBudget ") Then
        If e.Value IsNot Nothing Then

            If CType(e.Value, Integer) > -1 Then
                e.CellStyle.ForeColor = Color.Red
                'e.CellStyle.BackColor = Color.Red   'Or backcolor if you prefer...
            End If

        End If
    End If
End Sub
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

13 Experts available now in Live!

Get 1:1 Help Now