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

Technology Partners: 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

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 …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

691 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