Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hiii,

In my database i have table of color codes that is all hexadecimal values ..
now in gridview column i want to show one column in which i have labels . i want to set labels text as a color name and back color as that color.want to display display according colors in each row ..


C#
protected void gvAbsenceTypes_RowDataBound(object sender, GridViewRowEventArgs e)
        {   
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (DataBinder.Eval(e.Row.DataItem, "TotalLeave") != null)                
                {
                    if (DataBinder.Eval(e.Row.DataItem, "TotalLeave").ToString() != "0")
                    {
                        Label lblTotalLeave = (Label)e.Row.FindControl("lblTotalLeave");
                        lblTotalLeave.Text = DataBinder.Eval(e.Row.DataItem, "TotalLeave").ToString();
                    }
                }

                if (DataBinder.Eval(e.Row.DataItem, "colorcode").ToString() != "0")
                {
                    Label lblTotalLeave = (Label)e.Row.FindControl("lblColour");
                    lblTotalLeave.Text = System.Drawing.Color.FromName(DataBinder.Eval(e.Row.DataItem, "colorcode").ToString());
                }
            }
        }
Posted
Updated 17-Jul-13 23:03pm
v4
Comments
Torakami 18-Jul-13 3:58am    
I am trying this in rowdatabound ... How to do this
Dholakiya Ankit 18-Jul-13 4:15am    
you have posted code is it working?
Torakami 18-Jul-13 4:35am    
No no .. the color part is not working,,, I just showed what i am doing
Dholakiya Ankit 18-Jul-13 4:49am    
try my solution
Torakami 18-Jul-13 4:56am    
what is Text and backcolor ... ?? i didnt get it.. I want to set text as a color name and backcolor as a color..

You can set the background color of the label by using below call:

C#
lblTotalLeave.BackColor = System.Drawing.Color.FromName( DataBinder.Eval( e.Row.DataItem, "colorcode" ).ToString() );

lblTotalLeave.Text = System.Drawing.Color.FromName( DataBinder.Eval( e.Row.DataItem, "colorcode" )).Name.ToString();


I am sure this will solve your problem.
 
Share this answer
 
v3
Comments
Torakami 18-Jul-13 5:10am    
Can you show me how to display color name in the same label control with proper color
Torakami 18-Jul-13 5:10am    
want to display text property of this control as well
Arpit Shrivastava 18-Jul-13 10:10am    
You can use like this:

lblTotalLeave.Text = System.Drawing.Color.FromName( DataBinder.Eval( e.Row.DataItem, "colorcode" )).Name.ToString();
Torakami 20-Jul-13 4:00am    
its giving an error
Torakami 20-Jul-13 4:01am    
saying fromName has some invalid arguments
protected void gvAbsenceTypes_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (DataBinder.Eval(e.Row.DataItem, "TotalLeave") != null)
{
if (DataBinder.Eval(e.Row.DataItem, "TotalLeave").ToString() != "0")
{
Label lblTotalLeave = (Label)e.Row.FindControl("lblTotalLeave");
lblTotalLeave.Text = DataBinder.Eval(e.Row.DataItem, "TotalLeave").ToString();
}
}

if (DataBinder.Eval(e.Row.DataItem, "colorcode").ToString() != "0")
{
Label lblTotalLeave = (Label)e.Row.FindControl("lblColour");
lblTotalLeave.Style.Add("background-color",""+DataBinder.Eval(e.Row.DataItem, "colorcode").ToString()+"");
lblTotalLeave.Style.Add("color",""+DataBinder.Eval(e.Row.DataItem, "colorcode").ToString()+"");
lblTotalLeave.text=DataBinder.Eval(e.Row.DataItem, "colorcode").ToString();
}
}
}
 
Share this answer
 
v3
Comments
Torakami 18-Jul-13 5:31am    
want to display text property of this control as well
Dholakiya Ankit 18-Jul-13 5:37am    
updated check it now change datarow names
Dholakiya Ankit 18-Jul-13 5:38am    
text will shows color name and background color as well we have used style property of label
no need in row databound just bind the backcolor with label as it is a field in database
. In gridview template field you can do it in the label props. put hash before the colorcode in database or (use String.Formate if there is no hash symbol with colorcode in db). this is the easy approach
like this
C#
<asp:label id="lblColour" backcolor="<%#Eval("colorcode") %>" runat="server" xmlns:asp="#unknown"></asp:label>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900