RadGridView改变文本颜色

本文关键字:颜色 文本 改变 RadGridView | 更新日期: 2023-09-27 17:50:03

我有一个简单的RadGridView,其中我想更改特定行(= 3个单元格)的文本颜色。不幸的是,这段代码不起作用:

//add new row to the grid
EventLogGrid.Items.Add(new EventLogRow(eventType, occured, msg));
//change the color of the text of all cells if it's an exception
if (eventType == EventLogger.EventType.Exception)
{
  var rows = this.EventLogGrid.ChildrenOfType<GridViewRow>();
  rows.Last().Foreground = new SolidColorBrush(Colors.Yellow);
}

RadGridView改变文本颜色

实际上有一个非常简单的解决方案:

  1. 附加事件处理程序:

    EventLogGrid.RowLoaded += EventLogGrid_RowLoaded;
    
  2. 更改行颜色:

    if (((EventLogRow)e.DataElement).MsgType == EventLogger.EventType.Exception)
    {
       e.Row.Foreground = new SolidColorBrush(Colors.Red);
    }
    

试试这样的东西Telerik网站也有很多很棒的例子Telerik Radgrid Overview

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    //Check if the Item is a GridDataItem
    if (e.Item is GridDataItem)
    {
        GridDataItem dataBoundItem = e.Item as GridDataItem;
        if (int.Parse(dataBoundItem["yourColounName"].Text) == "Date")
        {
            dataBoundItem["yourColounName"].ForeColor = Color.Red;
            dataBoundItem["yourColounName"].Font.Bold = true;
        }
    }
}

或者这可以为你工作,我已经测试过了,如果你给我列名,请确保用你的列名替换列的名称,因为你有3列,我需要第一个列名。希望这对你有意义

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        TableCell cell = (TableCell)item["YourColumnName"];
        cell.BackColor = System.Drawing.Color.Yellow;
    }
}