Search code examples
objectlistview

ObjectListView, Change color of cell depending on property value


I use ObjectListView in a c# winforms project. My data models have a color-property. In the OLV there is a color-column where i want the backgrounds of the cells be set to the color value of the RowObject. How can i do this? For checkbox columns i can use BooleanCheckStatePutter. Maybe there is something like this?

Thanks in advance


Solution

  • You could use the FormatCell event like this:

        private void objectListView1_FormatCell(object sender, FormatCellEventArgs e) {
            // only apply formatting for the desired column
            if (e.ColumnIndex == olvColumn1.Index) {
                // get the model
                Item model = (Item)e.Model;
    
                // apply back color from model
                e.SubItem.BackColor = model.Color;                
            }
        }