2
votes

I'm using the Winform datagridview and try to customize my headers by changing the backcolor. In order to be able to modify the color, I must set the EnableHeadersVisualStyles property to false.

But when I set this property, I'm loosing the default 3D theme. Do you know if there is any workaround to save the 3D style and have the colored header?

My code:

dataGridTaxes.EnableHeadersVisualStyles = false;
dataGridTaxes.ColumnHeadersDefaultCellStyle = BlueDataGridHeaderStyle.GetInstance();

where BlueDataGridHeaderStyle is just a basic style with the new BackColor

1
Its all or nothing. Use the theming you get provided from the system via Visual Styles or paint yourself.Ralf

1 Answers

5
votes

You have to customize your DataGridView, here is a sample code I made for you. It looks really good compared with the default 3D style of ColumnHeader but supports BackColor and Hover backcolor:

    //CellPainting event handler for your dataGridView1
    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){
        if (e.RowIndex == -1 && e.ColumnIndex > -1){
            e.PaintBackground(e.CellBounds, true);
            RenderColumnHeader(e.Graphics, e.CellBounds, e.CellBounds.Contains(hotSpot) ? hotSpotColor : backColor);
            RenderColumnHeaderBorder(e.Graphics, e.CellBounds, e.ColumnIndex);
            using (Brush brush = new SolidBrush(e.CellStyle.ForeColor)){
                using (StringFormat sf = new StringFormat() {LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center}) {
                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, brush, e.CellBounds, sf);                        
                }
            }
            e.Handled = true;
        }
    }
    Color hotSpotColor = Color.LightGreen;//For hover backcolor
    Color backColor = Color.LimeGreen;    //For backColor    
    Point hotSpot;
    private void RenderColumnHeader(Graphics g, Rectangle headerBounds, Color c) {
        int topHeight = 10;
        Rectangle topRect = new Rectangle(headerBounds.Left, headerBounds.Top+1, headerBounds.Width, topHeight);
        RectangleF bottomRect = new RectangleF(headerBounds.Left, headerBounds.Top+1 + topHeight, headerBounds.Width, headerBounds.Height- topHeight-4);            
        Color c1 = Color.FromArgb(180, c);            
        using (SolidBrush brush = new SolidBrush(c1)) {
            g.FillRectangle(brush, topRect);
            brush.Color = c;
            g.FillRectangle(brush, bottomRect);
        }
    }
    private void RenderColumnHeaderBorder(Graphics g, Rectangle headerBounds, int colIndex) {            
        g.DrawRectangle(new Pen(Color.White, 0.1f), headerBounds.Left + 0.5f, headerBounds.Top + 0.5f,headerBounds.Width-1f,headerBounds.Height-1f);
        ControlPaint.DrawBorder(g, headerBounds, Color.Gray, 0, ButtonBorderStyle.Inset,
                                               Color.Gray, 0, ButtonBorderStyle.Inset,
                                             Color.Gray, colIndex != dataGridView1.ColumnCount - 1 ? 1 : 0, ButtonBorderStyle.Inset,
                                           Color.Gray, 1, ButtonBorderStyle.Inset);
    }
    //MouseMove event handler for your dataGridView1
    private void dataGridView1_MouseMove(object sender, MouseEventArgs e){
        hotSpot = e.Location;
    }
    //MouseLeave event handler for your dataGridView1
    private void dataGridView1_MouseLeave(object sender, EventArgs e){
        hotSpot = Point.Empty;
    }

enter image description here

You can implement the RenderColumnHeaderBorder as the followings using ControlPaint.DrawBorder3D instead, it's much simpler:

private void RenderColumnHeaderBorder(Graphics g, Rectangle headerBounds, int colIndex)
{
    ControlPaint.DrawBorder3D(g, headerBounds, Border3DStyle.Raised, Border3DSide.All & ~Border3DSide.Middle);
}

enter image description here

private void RenderColumnHeaderBorder(Graphics g, Rectangle headerBounds, int colIndex)
{
    ControlPaint.DrawBorder3D(g, headerBounds, Border3DStyle.RaiseInner, Border3DSide.All & ~Border3DSide.Middle);
}

enter image description here

private void RenderColumnHeaderBorder(Graphics g, Rectangle headerBounds, int colIndex)
{
    ControlPaint.DrawBorder3D(g, headerBounds, Border3DStyle.RaiseOuter, Border3DSide.All & ~Border3DSide.Middle);
}

enter image description here