ListView change cell forecolor

8 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
    2891 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. UI for WinForms is Visual Studio 2017 Ready
  4. 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;
  5. Answer
    Stefan
    Admin
    Stefan avatar
    2891 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 >>
  6. 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);
        }
     
    }
  7. 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
  8. 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.
  9. Dylan Lennox
    Dylan Lennox avatar
    3 posts
    Member since:
    Apr 2010

    Posted 13 Dec 2012 Link to this post

    thank you very much
Back to Top
UI for WinForms is Visual Studio 2017 Ready