本文介绍了改变前景色房颤的GridView细胞一个特殊字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变一些的特殊字词不会所有单词的颜色在GridView细胞。
下面是代码:

I want to change the color of some special words NOT all words in a gridview cell. Here is the code:

protected void gvContents_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[3].Text.Contains("Special"))
        {
            //set The "Special" word only forecolor to red
        }
        else if (e.Row.Cells[3].Text == "Perishable")
        {
            //set The "Perishable" word only forecolor to blue
        }
        else if (e.Row.Cells[3].Text == "Danger")
        {
            //set The "Danger" word only forecolor to yellow
        }
    }
}

和单元格文本可能会像在这里:放射性:危险或本人体:特殊,易腐。我应该怎么办?

and the cell text might be like here: Radioactive : Danger or this: Human Body : Special ,Perishable. What should I do?

推荐答案

使用的和CSS类。首先在你的aspx代码中创建CSS类:

Use a combination of span tags and CSS classes. First create the CSS classes in your aspx code:

<style>
    .redWord
    {
        color: Red;
    }
    .blueWord
    {
        color: Blue;
    }
    .yellowWord
    {
        color: Yellow;
    }
</style>



然后更换特殊来出现的所有<跨度类='redWord'>​​;特别< / SPAN> 寂灭< ;跨度类='blueWord'>​​;易腐< / SPAN> 危险<跨度类=' yellowWord'>​​;危险< / SPAN>

then replace all occurences of Special to <span class='redWord'>Special</span>, Perishable to <span class='blueWord'>Perishable</span>, and Danger to <span class='yellowWord'>Danger</span>:

protected void gvContents_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[3].Text = e.Row.Cells[3].Text.Replace("Special", "<span class='redWord'>Special</span>")
                              .Replace("Perishable", "<span class='blueWord'>Perishable</span>")
                              .Replace("Danger", "<span class='yellowWord'>Danger</span>");
    }
}

这篇关于改变前景色房颤的GridView细胞一个特殊字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:45