如何将backcolor设置为gridview的标签 [英] How to set backcolor to label of gridview

查看:120
本文介绍了如何将backcolor设置为gridview的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hiii,



在我的数据库中,我的颜色代码表都是十六进制值..

现在在gridview专栏中想要显示我有标签的一列。我想将标签文本设置为颜色名称,将背景颜色设置为该颜色。想要根据每行的颜色显示显示...





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


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());
                }
            }
        }

推荐答案

您可以使用设置标签的背景颜色以下电话:



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

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.


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)

{

标签lblTotalLeave =(标签)e.Row.FindControl(lblTotalLeave);

lblTotalLeave.Text = DataBinder.Eval(e.Row.DataItem,TotalLeave)。ToString();

}

}



if(DataBinder.Eval(e.Row.DataItem,colorcode)。ToString ()!=0)

{

标签lblTotalLeave =(标签)e.Row.FindControl(lblCo lour);

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();

}

}

}
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();
}
}
}


不需要行数据绑定只需将backcolor绑定到标签,因为它是数据库中的一个字段

。在gridview模板字段中,您可以在标签道具中执行此操作。将哈希值放在数据库中的colorcode之前或(如果db中没有带颜色代码的哈希符号,则使用String.Formate)。这是简单的方法

喜欢这个

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
<asp:label id="lblColour" backcolor="<%#Eval("colorcode") %>" runat="server" xmlns:asp="#unknown"></asp:label>


这篇关于如何将backcolor设置为gridview的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆