Solved

How to set different color for a databind value?

Posted on 2009-07-05
7
638 Views
Last Modified: 2013-12-17
Hi

I use the following ASP.NET to bind a data retrieved from DB.

The result is 10000 or -10000 (example)

How can I set the text color to be blue if it is -ve while the text color is red if it is +ve?

I use asp.net and C#.

 


<asp:Label ID="clientbalunfinish" runat="server" Text='<%# string.Format("{0:#,##0.00}", DataBinder.Eval(Container.DataItem, "clientbalunfinish"))  %>'></asp:Label>

Open in new window

0
Comment
Question by:techques
[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
  • 3
7 Comments
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 24781828
What control are you using to display the data? gridview? If its a gridview the you can attach a sub similiar to the following to the onRowDatabound event.
 if (e.Row.RowType == DataControlRowType.DataRow) {
        Label lbl = e.Row.FindControl("clientbalunfinish");
        if (lbl.Text.StartsWith("-")) {
            lbl.ForeColor = Drawing.Color.Red;
        }
        else {
            lbl.ForeColor = Drawing.Color.Blue;
        }
    }

Open in new window

0
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 24781837
Sorry forgot to enclose in the sub
public void gridview1_OnrowDatabound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) {
        Label lbl = e.Row.FindControl("clientbalunfinish");
        if (lbl.Text.StartsWith("-")) {
            lbl.ForeColor = Drawing.Color.Red;
        }
        else {
            lbl.ForeColor = Drawing.Color.Blue;
        }
    }
}

Open in new window

0
 
LVL 3

Expert Comment

by:sandip-mishra
ID: 24782104
hi,... it works fine for me.....

aspx.....

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label runat="server" ID="TextColor" Text='<%#DataBinder.Eval(Container.DataItem,"a") %>'></asp:Label>
                   
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="b" HeaderText="b" SortExpression="b" />
            <asp:BoundField DataField="c" HeaderText="c" SortExpression="c" />
            <asp:BoundField DataField="no" HeaderText="no" SortExpression="no" />
        </Columns>
    </asp:GridView>


aspx.cs......


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblColor = new Label();
            lblColor = (System.Web.UI.WebControls.Label)e.Row.FindControl("TextColor");
            if (lblColor.Text == "1")
            {
                lblColor.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                lblColor.ForeColor = System.Drawing.Color.Green;
            }
        }
    }

Regards

Sandip Mishra
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:techques
ID: 24782739
sorry, I use repeater to do it.


0
 

Author Comment

by:techques
ID: 24782850
I would need something like
<asp:Label ID="clientbalunfinish" runat="server" Text='<%# ((Convert.ToInt32(DataBinder.Eval(Container.DataItem, "clientbalunfinish")) > 0) ? "red text color" : "blue text color") + string.Format("{0:#,##0.00}",DataBinder.Eval(Container.DataItem, "clientbalunfinish"))%>'></asp:Label>

Open in new window

0
 
LVL 14

Accepted Solution

by:
Dustin Hopkins earned 50 total points
ID: 24783247
This should fit your needs.

Hope this helps,
Dustin
<asp:Label ID="clientbalunfinish" runat="server" ForeColor='<%# (System.Convert.ToInt32(DataBinder.Eval(Container.DataItem, "clientbalunfinish")) >= 0 ? System.Drawing.Color.Blue : System.Drawing.Color.Red) %>'  Text='<%# string.Format("{0:#,##0.00}",DataBinder.Eval(Container.DataItem, "clientbalunfinish")) %>'></asp:Label>

Open in new window

0
 

Author Closing Comment

by:techques
ID: 31599922
perfect solution! thanks
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

696 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