ListView change cell forecolor

11 posts, 1 answers
  1. Brandon
    Brandon avatar
    17 posts
    Member since:
    Sep 2011

    Posted 06 Jul 2012 Link to this post

    I have a list view that has numbers in the cells. If the number is negative I would like the text to turn red.

    Is the list view capable of this?

    I can change the row text color, but not a single cell.

    I am also using a ListViewDataItem variable as well.
  2. Stefan
    Admin
    Stefan avatar
    2901 posts

    Posted 10 Jul 2012 Link to this post

    Hi Brandon,

    Thank you for writing.

    The correct event for the desired purpose will be the VisualItemFormatting event. Here is a sample:
    void radListView1_VisualItemFormatting(object sender, ListViewVisualItemEventArgs e)
    {
        if (e.VisualItem.Data != null && Convert.ToInt16(e.VisualItem.Data["col1"]) < 0)
        {
            e.VisualItem.BackColor = Color.Red;
            e.VisualItem.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
        }
        else
        {
            e.VisualItem.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
            e.VisualItem.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
        }
    }

    I hope this helps. Should you have any other questions, do not hesitate to contact us.
     
    Regards,
    Stefan
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  3. Brandon
    Brandon avatar
    17 posts
    Member since:
    Sep 2011

    Posted 10 Jul 2012 Link to this post

    The code below colors the entire line red..

    I only want to color a single cell if the cell value is red.

    e.VisualItem.BackColor = Color.Red;
  4. Answer
    Stefan
    Admin
    Stefan avatar
    2901 posts

    Posted 13 Jul 2012 Link to this post

    Hello Brandon,

    Please excuse me for the confusion. I do now know why I though you want to style the whole row.

    However, here is the answer - to style a cell you need to use the CellFormatting event of the control. Here is a sample:
    void radListView1_CellFormatting(object sender, ListViewCellFormattingEventArgs e)
    {
        DetailListViewDataCellElement cell = e.CellElement as DetailListViewDataCellElement;
     
        if (cell != null && cell.Data.Name == "col1" && Convert.ToInt32(cell.Row["col1"]) < 0)
        {
            cell.BackColor = Color.Yellow;
            cell.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
        }
    }

    Let me know if there is anything else I can do for you.
     
    All the best,
    Stefan
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  5. Brandon
    Brandon avatar
    17 posts
    Member since:
    Sep 2011

    Posted 05 Oct 2012 Link to this post

    That was what I needed.

    To further extend on your solution I have dynamically named columns except for the first 2 columns. This would be how to do it with dynamically named columns:

    private void radListView1_CellFormatting(object sender, ListViewCellFormattingEventArgs e)
    {
        DetailListViewDataCellElement cell = e.CellElement as DetailListViewDataCellElement;
     
     
        if (cell != null && cell.Data.Name != "col1" && cell.Data.Name != "col2" && Convert.ToDecimal(cell.Row[cell.Data.Name].ToString()) < 0)
        {
            cell.ForeColor = Color.Red;
            cell.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
        }
     
    }
  6. Dylan Lennox
    Dylan Lennox avatar
    3 posts
    Member since:
    Apr 2010

    Posted 10 Dec 2012 Link to this post

    hello i see how to change the selected color but how do you change the border of the selected item aswell but only the selected border not all borders

    ill try to be more specific when you select the item it creates a border around it i would like to change that
  7. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 13 Dec 2012 Link to this post

    Hi Dylan,

    Thank you for contacting us.

    If you are not using DetailsView, then you should handle the VisualItemFormatting event as shown below:
    void radListView1_VisualItemFormatting(object sender, Telerik.WinControls.UI.ListViewVisualItemEventArgs e)
    {
        if (e.VisualItem.Data.Selected)
        {
            e.VisualItem.BorderColor = Color.Red;
            e.VisualItem.BorderGradientStyle = Telerik.WinControls.GradientStyles.Solid;
        }
        else
        {
            e.VisualItem.ResetValue(LightVisualElement.BorderColorProperty, Telerik.WinControls.ValueResetFlags.Local);
            e.VisualItem.ResetValue(LightVisualElement.BorderGradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
        }
    }

    In case you are using DetailsView, then you should handle the CellFormatting event as the following code snippet demonstrates:
    void radListView1_CellFormatting(object sender, ListViewCellFormattingEventArgs e)
    {
        DetailListViewDataCellElement dataCell =  e.CellElement as DetailListViewDataCellElement;
     
        if (dataCell != null && dataCell.Row.Selected)
        {
            dataCell.BorderColor = Color.Red;
            dataCell.BorderGradientStyle = Telerik.WinControls.GradientStyles.Solid;
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.BorderColorProperty, Telerik.WinControls.ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.BorderGradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
        }
    }

    I hope this helps. Feel free to ask if you have any additional questions.

    Regards,
    Ivan Todorov
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  8. Dylan Lennox
    Dylan Lennox avatar
    3 posts
    Member since:
    Apr 2010

    Posted 13 Dec 2012 Link to this post

    thank you very much
  9. Ammar
    Ammar avatar
    21 posts
    Member since:
    May 2015

    Posted 13 Apr Link to this post

    Hello dear,

    I want only to change FontStyle and ForeColor for a single cell depend on a such value, so if there possibllity to doing this out of 

    radListView1_CellFormatting event, check my code:

    Dim item As ListViewDataItem
     
    item = New ListViewDataItem
    item(0) = "Color Red"
    item(1) = "Red"
     
    RadListView1.Items.Add(item)
    item = Nothing

     

    so i need to something like this:

    item(1).ForeColor = Color.Red
    item(1).Font = New Font("Arial", 8.25, FontStyle.Bold)

     

    I hope you can help me, regards.

  10. Dimitar
    Admin
    Dimitar avatar
    1650 posts

    Posted 14 Apr Link to this post

    Hello Ammar,

    Since RadListView is using UI Virtualization you cannot style the items outside of the formatting events. Please note that you have access to the data item in the formatting events. An example is available here: Formatting Items.

    I hope this will be useful. Let me know if you have additional questions.

    Regards,
    Dimitar
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  11. Ammar
    Ammar avatar
    21 posts
    Member since:
    May 2015

    Posted 16 Apr in reply to Dimitar Link to this post

    thanks dear.

Back to Top