datagridviewcombobox

How can I add custom colors to a datagridviewcombobox cell based on the user selection?


I want to change the background and foreground color in a datagridviewcombobox based on what the user selects. I have tried a bunch of variations on the following:

Private Sub dgvSchedule_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvSchedule.CellFormatting
        If e.Value IsNot Nothing Then
            If e.ColumnIndex Mod 3 = 0 Then
                Dim cellValue As String = e.Value.ToString()
                Select Case cellValue
                    Case "<No one> "
                        e.CellStyle.BackColor = Color.DarkGray
                        e.CellStyle.ForeColor = Color.LightGray
                    Case Else
                        e.CellStyle.BackColor = Color.White
                        e.CellStyle.ForeColor = Color.Black
                End Select
            End If
        End If
    End Sub

I have tried similar code triggered by CellValueChanged, CellFormatting, SelectedIndexChanged, cellEndEdit. I have set EnableHeadersVisualStyles = False. I have checked that the formatting lines are hit appropriately. In some cases, the dropdown list color changes based on the current selection, but when the dropdown list closes, everything is plain black on white again. Any suggestions would be appreciated. Thank you!


Solution

  • It's not much, but I hope this can help you.

    namespace StackOverflow
    {
        public partial class Form1 : Form
        {
            enum Choices
            {
                First,
                Second,
                Third
            }
            DataGridView dgv = new();
            public Form1()
            {
                InitializeComponent();
                Controls.Add(dgv);
                dgv.Dock = DockStyle.Fill;
                dgv.Columns.Add(new DataGridViewComboBoxColumn() { Name = "ChoiceCol", ValueType = typeof(Choices), DataSource = Enum.GetValues(typeof(Choices)) });
    
                dgv.CellFormatting += Dgv_CellFormatting;
    
            }
    
            private void Dgv_CellFormatting(object? sender, DataGridViewCellFormattingEventArgs e)
            {
                if (dgv[e.ColumnIndex, e.RowIndex].Value != null)
                {
    
                    if (dgv.Columns[e.ColumnIndex].Name == "ChoiceCol")
                    {
                        switch ((Choices)dgv[e.ColumnIndex, e.RowIndex].Value)
                        {
                            case Choices.First:
                                ((DataGridViewComboBoxCell)dgv[e.ColumnIndex, e.RowIndex]).DisplayStyleForCurrentCellOnly = true;
                                ((DataGridViewComboBoxCell)dgv[e.ColumnIndex, e.RowIndex]).Style.BackColor = Color.Green;
                                dgv[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Blue;
                                break;
                            case Choices.Second:
                                ((DataGridViewComboBoxCell)dgv[e.ColumnIndex, e.RowIndex]).DisplayStyleForCurrentCellOnly = true;
                                dgv[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Orange;
                                dgv[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Red;
                                break;
                            case Choices.Third:
                                ((DataGridViewComboBoxCell)dgv[e.ColumnIndex, e.RowIndex]).DisplayStyleForCurrentCellOnly = true;
                                dgv[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Yellow;
                                dgv[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Cyan;
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
    }
    

    enter image description here