4

I have a simple RadGridView in which I want to change the color of the text for a specific row (= 3 cells). Unfortunately, this code doesn't work:

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

Any input would be appreciated.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Mike
  • 1,992
  • 4
  • 31
  • 42
  • What is the mean "Doesn't work"? What is the error? Be more spesific. – Soner Gönül Dec 31 '12 at 15:50
  • Have you looked at the documentation on Telerik site..? they have a ton of working examples – MethodMan Dec 31 '12 at 15:53
  • @Soner Gönül: There is no error, it simply doesn't change the color of the text. – Mike Dec 31 '12 at 15:55
  • Mike why not use this event to start writing the code to change the color.. protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e){ } – MethodMan Dec 31 '12 at 15:57
  • @DJ KRAZE: ok, thanks, will try it right now. – Mike Dec 31 '12 at 15:59
  • @Mike, are you sure `EventLogRow` is changing eventType to that type of Exception? If so, are you sure an object is being created/returned (I would assume so). Lastly, I would think you are meaning to do `new EventLogRow(ref eventType, occured,msg)` or am I wrong? – Kcvin Dec 31 '12 at 16:45
  • I think the problem is not EventLogRow, because this is just a very simple object with 3 string properties (=3 cells). My code works fine, except for the color change. – Mike Dec 31 '12 at 16:53

2 Answers2

4

There's actually a very simple solution:

  1. Attach an event handler:

    EventLogGrid.RowLoaded += EventLogGrid_RowLoaded;
    
  2. Change the color of the row:

    if (((EventLogRow)e.DataElement).MsgType == EventLogger.EventType.Exception)
    {
       e.Row.Foreground = new SolidColorBrush(Colors.Red);
    }
    
Mike
  • 1,992
  • 4
  • 31
  • 42
Roland
  • 488
  • 1
  • 4
  • 13
1

Try something like this also Telerik website has tons of awesome examples 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;
        }
    }
}

or this can work for you I've tested this and it work make sure to replace the name of the Columns with your Column Name if you give me the column name, since you have 3 Columns I would need the Fist Column name.. hope this makes sense for you

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;
    }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • ok, I've tested this code and it does not work. It seems that you are using the Telerik web library, but I'm using the Telerik WPF lib which seems to be different. Thanks anyway. – Mike Dec 31 '12 at 17:03
  • I have the same lib I will correct my answer and give you a working solution – MethodMan Dec 31 '12 at 19:34
  • What is the name of your first Column in the RadGrid.. then I can post an example of what you should do.. thanks – MethodMan Dec 31 '12 at 19:43