how to set the color of dividerwidth in column header

1.4k views Asked by At

I've set the divider width and divider height to non-zero and then used dataGridview1.GridColor = Color.Red to set the color of the dividers. This doesn't affect the header though. How can I change the color of the gap between the header cells?; i.e. How can I make that gap Red also?

datagrid example with white divider

1

There are 1 answers

0
TaW On BEST ANSWER

Update: The trick is to allow your own styles to be applied in the Headers. To do this you need this line to turn off the EnableHeadersVisualStyles flag:

  dataGridView1.EnableHeadersVisualStyles = false;

Without it the user settings are applied. See MSDN


Old answer:

You can always do stuff by owner-drawing the header cells.

Here is a short example:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex >= 0) return;  // only the column headers!
    // the hard work still can be done by the system:
    e.PaintBackground(e.CellBounds, true);
    e.PaintContent(e.CellBounds);
    // now for the lines in the header..
    Rectangle r = e.CellBounds;
    using (Pen pen0 = new Pen(dataGridView1.GridColor, 1))
    {
        // first vertical grid line:
        if (e.ColumnIndex < 0) e.Graphics.DrawLine(pen0, r.X, r.Y, r.X, r.Bottom);
        // right border of each cell:
        e.Graphics.DrawLine(pen0, r.Right - 1, r.Y, r.Right - 1, r.Bottom);
    }
    e.Handled = true;  // stop the system from any further work on the headers
}

enter image description here