This is a migrated thread and some comments may be shown as answers.

ListView change cell forecolor

13 Answers 479 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Brandon
Top achievements
Rank 1
Brandon asked on 06 Jul 2012, 06:24 PM
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.

13 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 10 Jul 2012, 12:07 PM
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 >>
0
Brandon
Top achievements
Rank 1
answered on 10 Jul 2012, 09:11 PM

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;
0
Accepted
Stefan
Telerik team
answered on 13 Jul 2012, 01:16 PM
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 >>
0
Brandon
Top achievements
Rank 1
answered on 05 Oct 2012, 02:45 PM
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);
    }
 
}
0
Dylan Lennox
Top achievements
Rank 1
answered on 11 Dec 2012, 02:56 AM
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
0
Ivan Todorov
Telerik team
answered on 13 Dec 2012, 05:09 PM
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.
0
Dylan Lennox
Top achievements
Rank 1
answered on 13 Dec 2012, 07:06 PM
thank you very much
0
Ammar
Top achievements
Rank 1
answered on 13 Apr 2017, 01:57 PM

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.

0
Dimitar
Telerik team
answered on 14 Apr 2017, 06:35 AM
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.
0
Ammar
Top achievements
Rank 1
answered on 16 Apr 2017, 08:09 AM

thanks dear.

0
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 17 Oct 2020, 10:45 AM

Hello

the previous posts help me a lot, but I need to change the backcolor of the cell based on the value of another cell in the same row.

and I fail to find how to access another cell in the same row ...

Thanks in advance

Pierre-Jean

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Oct 2020, 09:18 AM

Hello, Pierre-Jean, 

I have prepared a sample code snippet demonstrating how to access the DataBoundItem associated with the row and from there to access a value of another cell (e.g. "CategoryID"):
        public RadForm1()
        {
            InitializeComponent();
            this.radListView1.CellFormatting += radListView1_CellFormatting;
        }

        private void radListView1_CellFormatting(object sender, ListViewCellFormattingEventArgs e)
        {
            DetailListViewDataCellElement dataCell = e.CellElement as DetailListViewDataCellElement;
            if (dataCell != null && e.CellElement.Data.Name == "ProductName")
            {
                DataRowView rowView = dataCell.Row.DataBoundItem as DataRowView;
                if ((int)rowView.Row["CategoryID"] == 1)
                {
                    e.CellElement.BackColor = Color.Yellow;
                    e.CellElement.DrawFill = true;
                    e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
                }
                else
                { 
                    e.CellElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
                    e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local);
                    e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local); 
                }
            }
            else
            { 
                e.CellElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local); 
            }
        }

        private void RadForm1_Load(object sender, EventArgs e)
        { 
            this.productsTableAdapter.Fill(this.nwindDataSet.Products);

            this.radListView1.DataSource = this.productsBindingSource;
            this.radListView1.ViewType = ListViewType.DetailsView;
        }

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 24 Oct 2020, 03:49 PM

Hello

Just what I was missing

Thanks a lot

Pierre-Jean

Tags
ListView
Asked by
Brandon
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Brandon
Top achievements
Rank 1
Dylan Lennox
Top achievements
Rank 1
Ivan Todorov
Telerik team
Ammar
Top achievements
Rank 1
Dimitar
Telerik team
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or