Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to set different color for a databind value?

Posted on 2009-07-05
7
Medium Priority
?
641 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 200 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

618 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