Question

I want to Get the DataGridView Header Cell's Background color, I have done a trick but its giving me empty and RGB = 0,0,0

I have try this code:

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

I have to repaint Header Cell's Background of the same color as it has before repainting with size modifications .

Kindly Suggest a Solution, I have searched a lot but no useful help

Était-ce utile?

La solution 2

After some efforts, I finally write down code with some suggestions Its a Generic Code that can be call at any Grid's Paint Method of C# WinForm, pass it the Grid,The Columns's names, and the paint Graphics object

I have added a fill rectangle of size 4, that starts from the previous column's Right-2 to Next Column's Left+2 , So it hides the Vertical Bar

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

Autres conseils

The DataGridView uses style inheritance, so unless an individual header cell differs from the default, its BackColor will be set to Color.Empty.

Try the ColumnHeadersDefaultCellStyle.BackColor property instead.

Note that when EnableHeadersVisualStyles is set to true (the default), the value will be ignored and the headers will be drawn using a visual style renderer instead.

I have found this working well. You need to set EnableHeadersVisualStyles=fasle; I have tested in both the ways:

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;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top