Search code examples
vb.netcolorsdatagridviewborderseparator

Data grid view header Grid color


This is a VB .NET application where we are showing the output of a SQL statement in a Datagrid view. I'm using .NET 2005.

We need to get the separators of the headers on the grid control to be the same colors as the GridColor on the form.

enter image description here

We've tried looking through all of the properties of the DataGridView control, and found some interesting things that looked promising such as the DataGridViewAdvancedHeaderStyle, and DataGridViewHeaderBorderStyle, but none of it seems to allow you to change the colors on it.

Does anyone know how to do this without remaking the entire thing with a GDI+ control?


Solution

  • Well, I never did find a property for this, so I ended up creating a custom component, and overloading the OnPaint event handler to draw a line over the existing one.

    Here is the code for it if anyone else ever comes across this post looking for a solution:

    Private Sub CustomDataGridView_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim g As Graphics = e.Graphics
        Dim pen As New Pen(Me.GridColor)
        Dim TWidth As Integer = 2
        Dim HeaderWidth As Integer = 0
        If Me.RowHeadersVisible Then
            HeaderWidth = Me.RowHeadersWidth
        End If
        For Each column As DataGridViewColumn In Me.Columns
            Dim x As Integer = HeaderWidth + TWidth - 1
            TWidth += column.Width
            Dim top As Integer = column.HeaderCell.ContentBounds.Top
            Dim bottom As Integer = column.HeaderCell.ContentBounds.Bottom + 1
            pen.Width = 2
            g.DrawLine(pen, x, top, x, bottom)
        Next column
    End Sub