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,120 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
[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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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