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,119 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

839 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