DatGridView Header Background Color

I want to get the background color of the DataGridView header, I did the trick, but it gave me a blank and RGB = 0,0,0

I tried this code:

 Color cl = dataGridView1.Columns["<Column>"].HeaderCell.Style.BackColor; //<AnyColumn>

I need to redraw the background of the header cell of the same color as before redrawing with resizing.

Please offer a solution, I searched a lot, but not useful help

+3
source share
3 answers

After some effort, I finally write down the code with some sentences. Its general code, which can be called using any Grid Paint method for C # WinForm, give it the names Grid, The Columns and the Graphics Paint object

I added a fill rectangle of size 4, which starts from the previous column on the right-2 to the next column on the left + 2, so it hides the vertical panel

Public Sub VerticalBarHide(ByVal grd As KryptonExtendedGrid, ByVal colname As String(), ByVal e As System.Drawing.Graphics)
    Dim rectHeader As Rectangle
    grd.EnableHeadersVisualStyles = False
    Dim bgColor As Color
    bgColor = grd.ColumnHeadersDefaultCellStyle.BackColor
    For Each name As String In colname
        rectHeader = grd.GetCellDisplayRectangle(grd.Columns(name).Index, -1, True)
        rectHeader.X = rectHeader.X + rectHeader.Width - 2
        rectHeader.Y += 1
        rectHeader.Width = 2 * 2
        rectHeader.Height -= 2
        e.FillRectangle(New SolidBrush(bgColor), rectHeader)
    Next

End Sub
+2

DataGridView , , , BackColor Color.Empty.

ColumnHeadersDefaultCellStyle.BackColor.

, EnableHeadersVisualStyles true ( ), , .

+3

I found this to work well. You need to install. EnableHeadersVisualStyles=fasle; I tested both methods:

dataGridView1.EnableHeadersVisualStyles = false;

DataGridViewColumn dataGridViewColumn = dataGridView1.Columns["Column1"];
dataGridViewColumn.HeaderCell.Style.BackColor = Color.Magenta;
dataGridViewColumn.HeaderCell.Style.ForeColor = Color.Yellow;

Color cl = dataGridViewColumn.HeaderCell.Style.BackColor;
//or   
Color cl2 = dataGridView1.Columns["Column1"].HeaderCell.Style.BackColor;
+1
source

All Articles