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,116 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
ID: 18858730
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
ID: 18858784
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
ID: 18858800
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
ID: 18859081
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
ID: 18859252
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Moving code from one TFS branch to another 3 25
VB.net Open Folder in Windows Explorer 3 24
write xml in vb.net 2 24
vb.net and creating a class 5 14
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

785 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