Solved

How to set different color for a databind value?

Posted on 2009-07-05
7
635 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
  • 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now