【问题标题】:RadGridView change text colorRadGridView 更改文本颜色
【发布时间】:2012-12-15 17:27:37
【问题描述】:

我有一个简单的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);
}

我们将不胜感激。

【问题讨论】:

  • “不起作用”是什么意思?错误是什么?更具体。
  • 您查看过 Telerik 网站上的文档吗?他们有大量的工作示例
  • @Soner Gönül:没有错误,它根本不会改变文本的颜色。
  • Mike 为什么不用这个事件开始编写代码来改变颜色.. protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e){ }
  • @DJ KRAZE:好的,谢谢,马上试试。

标签: c# wpf telerik telerik-grid radgridview


【解决方案1】:

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

或者这对你有用..希望这对你有意义

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

【讨论】:

  • 好的,我已经测试了这段代码,但它不起作用。看来您使用的是 Telerik Web 库,但我使用的是 Telerik WPF 库,这似乎有所不同。还是谢谢。
  • 我有相同的库,我会更正我的答案并给你一个可行的解决方案
  • 你在 RadGrid 中的第一列的名称是什么。然后我可以发布一个你应该做什么的例子。谢谢
【解决方案2】:

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

  1. 附加事件处理程序:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2016-11-07
    • 2017-12-09
    • 2014-11-18
    相关资源
    最近更新 更多