【问题标题】:ASP.NET: How to change color of certain texts in a gridview?ASP.NET:如何更改网格视图中某些文本的颜色?
【发布时间】:2010-02-23 03:38:49
【问题描述】:

我有一个显示来自数据库的数据的网格视图...目前我只能更改单元格中所有文本的颜色,但我需要做的只是更改某些文本的颜色。有没有办法做到这一点?这可能吗?我只是一个新手,所以请耐心等待我..

这是我更改单元格中文本颜色的功能:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
{  
    if (_SearchKey != string.Empty)  
    {  
        if (e.Row.RowType == DataControlRowType.DataRow)  
        {  
            for (int ctr = 0; ctr < e.Row.Cells.Count; ctr++)  
            {  
                if (e.Row.Cells[ctr].Text.ToLower().Contains(_SearchKey.ToLower()))  
                {  
                    e.Row.Cells[ctr].ForeColor = System.Drawing.Color.Red;  
                }  
            }  
        }  
    }  
}  

【问题讨论】:

    标签: asp.net gridview text colors


    【解决方案1】:

    看看这个教程

    另一种方法是在 GridView 控件中使用Template Field

    【讨论】:

    • 对不起,先生,但我还是不明白...我知道它可以根据数据进行格式化,但我需要做的是在 gridview 的单元格中格式化整个文本的某些部分.例如,以下文本位于网格视图的单元格中:“软件和硬件”。如果搜索键是“ware”,则文本“ware”的两个实例将变为红色,但文本“Soft”、“and”、“Hard”仍将为黑色......“Sofware
    • 这是一个不同的要求。你介意使用javascript吗?这是nsftools.com/misc/SearchAndHighlight.htmtedpavlic.com/post_simple_inpage_highlighting_example.php的文章
    【解决方案2】:

    试试这个

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
    
            if (e.Row.RowType != DataControlRowType.DataRow) return;
    
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                if (e.Row.Cells[i].Text.ToLower().IndexOf("YourSearchItemHere", StringComparison.Ordinal) > -1)
                {
                    e.Row.Cells[i].ForeColor = Color.Red;
                }
            }
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-01
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多