Search code examples
c#.netwinformscustom-controlsdatagridviewcombobox

How to paint inactive rows in DataGridViewComboBox column?


I can easily paint items in DataGridViewComboBox dropdown list. But I can't figure out how to paint inactive cells in the same column.

Example of DataGridViewComboBox

I've seen, studied and tried numberous examples for classical ComboBox, but I don't understand all aspects of DataGridViewComboBox.

Currently I have DataGridViewCustomPaintComboBox class derived from DataGridViewComboBox. What is the minimum set of overrides to supply? Can you point me in right direction?


Solution

  • The required minimum to paint inactive cells without the focus seems to be CellTemplate assignment and Paint() override:

    public class DataGridViewCustomPaintComboBox : DataGridViewComboBoxColumn
    {
    
        public DataGridViewCustomPaintComboBox()
        {
            base.New();
            CellTemplate = new DataGridViewCustomPaintComboBoxCell();
        }
    
    }
    
    public class DataGridViewCustomPaintComboBoxCell : DataGridViewComboBoxCell
    {
    
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            // modify received arguments here
            base.Paint(...); // paint default parts (see paintParts argument)
            // add any custom painting here
    
        }
    
    }