Get backcolor and forecolor of SubItem

2 posts, 0 answers
  1. Lauren
    Lauren avatar
    2 posts
    Member since:
    Mar 2014

    Posted 18 Jun 2014 Link to this post

    I'm setting the backcolor and forecolor of SubItems in the radListView1_CellFormatting event.  Later, I'm exporting my List View to Excel, and I need to access the backcolor and forecolor of each SubItem/cell.  Is this possible?  I see the property for Item backcolor, but I can't get at the SubItem backcolor.
  2. George
    George avatar
    500 posts

    Posted 23 Jun 2014 Link to this post

    Hello Lauren,

    Thank you for writing.

    You can create a caching mechanism to store the colors. This will require creating a unique key to retrieve the colors from a dictionary:
    Dictionary<object, Color[]> cache = new Dictionary<object, Color[]>();
    void listView_CellFormatting(object sender, ListViewCellFormattingEventArgs e)
        RadListViewElement element = sender as RadListViewElement;
        if (e.CellElement is DetailListViewDataCellElement)
            DetailListViewDataCellElement cell = e.CellElement as DetailListViewDataCellElement;
            ListViewDetailColumn column = cell.Data;
            object keyItem = cell.Row[element.Columns.IndexOf(column)] + column.FieldName;
            if (!cache.ContainsKey(keyItem))
                cache[keyItem] = new Color[2];
            e.CellElement.BackColor = cache[keyItem][0] = Color.Red;
            e.CellElement.ForeColor = cache[keyItem][1] = Color.Blue;

    When you want to retrieve the colors you can do it by accessing the Columns and the Items collection of RadListView, as each Item in the Items collection is the Row of a cell.

    Do not hesitate to write back, should you have further questions.

    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
Back to Top