techques
asked on
How to set different color for a databind value?
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#.
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>
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;
}
}
}
hi,... it works fine for me.....
aspx.....
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False "
DataSourceID="SqlDataSourc e1" onrowdatabound="GridView1_ RowDataBou nd">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="TextColor" Text='<%#DataBinder.Eval(C ontainer.D ataItem,"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(obj ect sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
Label lblColor = new Label();
lblColor = (System.Web.UI.WebControls .Label)e.R ow.FindCon trol("Text Color");
if (lblColor.Text == "1")
{
lblColor.ForeColor = System.Drawing.Color.Red;
}
else
{
lblColor.ForeColor = System.Drawing.Color.Green ;
}
}
}
Regards
Sandip Mishra
aspx.....
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False
DataSourceID="SqlDataSourc
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="TextColor" Text='<%#DataBinder.Eval(C
</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(obj
{
if (e.Row.RowType == DataControlRowType.DataRow
{
Label lblColor = new Label();
lblColor = (System.Web.UI.WebControls
if (lblColor.Text == "1")
{
lblColor.ForeColor = System.Drawing.Color.Red;
}
else
{
lblColor.ForeColor = System.Drawing.Color.Green
}
}
}
Regards
Sandip Mishra
ASKER
sorry, I use repeater to do it.
ASKER
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
perfect solution! thanks
Open in new window