开发者

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. using .NET 2005.

We need to get 开发者_如何学Pythonthe seperators of the headers on the grid control to be the same colors as the GridColor on the form. See the picture below:

Data grid view header Grid color

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?


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


To change the backcolor of the Column Headers in a datagridview, choose False for EnableHeadersVisualStyles. Then open ColumnHeadersDefaultCellStyle and choose the background color.


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

DataGridView.ColumnBordersHeaderStyle
DataGridView.RowBordersHeaderStyle
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜