Data grid view header Grid color

2019-03-01 11:32发布

问题:

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

We need to get the seperators of the headers on the grid control to be the same colors as the GridColor on the form. See the picture below:

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?

回答1:

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


回答2:

I can't see the picture but what about playing with these?

DataGridView.ColumnBordersHeaderStyle
DataGridView.RowBordersHeaderStyle