C# 在gridview单元格中更改前景色为特殊单词

C# 在gridview单元格中更改前景色为特殊单词,c#,css,asp.net,gridview,colors,C#,Css,Asp.net,Gridview,Colors,我想更改gridview单元格中某些特殊单词的颜色。 代码如下: protected void gvContents_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[3].Text.Contains("Special")) { //s

我想更改gridview单元格中某些特殊单词的颜色。 代码如下:

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

单元格文本可能如下:
放射性:危险
或此:
人体:特殊、易腐
。我应该怎么做?

在CellFormatting事件处理程序中,添加以下代码

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.Value != null && e.Value.ToString() == "Special")
        {
            e.CellStyle.ForeColor = Color.Red;
        }
    }
使用和CSS类的组合。首先在aspx代码中创建CSS类:

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

谢谢Juniaith,但是这个代码改变了整个单元格的颜色,我只想改变那个单词的颜色!!如果要更改特定单词的颜色,必须使用与给定RichEditTextColumn类似的自定义列
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>");
    }
}