Solved

How to set different color for a databind value?

Posted on 2009-07-05
7
636 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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