Solved

How to set different color for a databind value?

Posted on 2009-07-05
7
637 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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