Search code examples
c#winformsdatagridviewdatagridviewcomboboxcell

Show DataGridViewComboBoxColumn in 3d Style


I was planning to make the DataGridViewComboboxCell to display similar to a 3D Fixed Style of a textbox. I manage to do it with a Combobox using this code:

    public Form1()
    {
        cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
        cmbbox.DrawItem += ComboBox_DrawItem_3DFixed;
    }

    private void ComboBox_DrawItem_3DFixed(object sender, DrawItemEventArgs e)
    {
        ComboBox cmb = sender as ComboBox;

        e.DrawBackground();
        if (e.State == DrawItemState.Focus)
            e.DrawFocusRectangle();

        var index = e.Index;
        if (index < 0 || index >= cmb.Items.Count)
            return;

        var item = cmb.Items[index];
        string text = (item == null) ? "(null)" : cmb.GetItemText(item);
        using (var brush = new SolidBrush(e.ForeColor))
        {
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            e.Graphics.DrawString(text, e.Font, brush, e.Bounds);
        }
    }

Unfortunately, I don't know how to do it with the DataGridViewComboboxCell. Tho I did found a solution here:

    public void Form1()
    {
        dgView.CellPainting += dgView_EditingControlShowing;
    }

    void dgView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control is ComboBox)
        {
            ComboBox cb = (ComboBox)e.Control;
            cb.DrawMode = DrawMode.OwnerDrawFixed;
            cb.DrawItem += new DrawItemEventHandler(ComboBox_DrawItem_3DFixed);
        }
    }

But the problem with this, it only changes the appearance of the DataGridViewComboboxCell when the specific cell is clicked, and when it lose focus, it returns back to normal.

I did find the CellPainting Event, but I don't know how it works for this code. Can anyone help me? Thanks!


Solution

  • To create a 3D style DataGridViewComboBoxColumn You should perform these 2 settings:

    1. You should disable visual styles for combo box editing control
    2. You should draw combo box cell yourself and draw a 3d combo button

    To do so, handle EditingControlShowing and CellPaint event:

    [DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
    static extern int SetWindowTheme(IntPtr hWnd, String pszSubAppName, String pszSubIdList);
    void dataGridView1_EditingControlShowing(object sender,
        DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control is ComboBox)
            SetWindowTheme(e.Control.Handle, "", "");
    }
    void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.ColumnIndex >= 0 && e.RowIndex >= 0 &&
            this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn)
        {
            var r1 = e.CellBounds;
            using (var brush = new SolidBrush(e.CellStyle.BackColor))
                e.Graphics.FillRectangle(brush, r1);
            r1.Width --;
            ControlPaint.DrawBorder3D(e.Graphics, r1, Border3DStyle.Sunken);
            e.Paint(r1, DataGridViewPaintParts.Border |
                DataGridViewPaintParts.ContentForeground);
            var d = SystemInformation.VerticalScrollBarWidth;
            var r2 = new Rectangle(r1.Right - d - 2, r1.Top + 2, d, r1.Height - 5);
            ControlPaint.DrawComboButton(e.Graphics, r2, ButtonState.Normal);
            e.Handled = true;
        }
    }
    

    enter image description here

    Also to create below appearance, without any customization code, it's enough to set DisplayStyle of your column to ComboBox:

    enter image description here