How to change the color of winform DataGridview header?
The way to do this is to set the EnableHeadersVisualStyles
flag for the data grid view to False
, and set the background colour via the ColumnHeadersDefaultCellStyle.BackColor
property. For example, to set the background colour to blue, use the following (or set in the designer if you prefer):
_dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;
_dataGridView.EnableHeadersVisualStyles = false;
If you do not set the EnableHeadersVisualStyles
flag to False, then the changes you make to the style of the header will not take effect, as the grid will use the style from the current users default theme. The MSDN documentation for this property is here.
How to change the datagridView Header color
In datagridView you can change the Header color by using DataGridViewCellStyle, see the following code
' Set the selection background color for all the cells.
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black
' Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default
' value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty
' Set the background color for all rows and for alternating rows.
' The value for alternating rows overrides the value for all rows.
dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkGray
' Set the row and column header styles.
dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Black
dataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Black
EDIT:
Using the DataGridViewCellStyle, your header color will changes but a seperator for columns in the header section will not appear. So, heres a overrided event of OnPaint Event Handler have a look at this
Hot to change specific column header color only in datagridview?
First in your DataGridView you need to set EnableHeadersVisualStyles to false.
After you've done that you can set the individual header style on each column.
DataGridViewColumn dataGridViewColumn = dataGridView1.Columns[0];
dataGridViewColumn.HeaderCell.Style.BackColor = Color.Magenta;
dataGridViewColumn.HeaderCell.Style.ForeColor = Color.Yellow;
Change color of datagridview rowheader of column header C#
The Cell
is called TopLeftHeaderCell
and you can set its Value
like this:
dataGridView1.TopLeftHeaderCell.Value = "HIHO";
It even has a Style
property with Colors
etc..but I found that changing the BackColor
has no effect. That is unless you change this obscure setting to false
:
dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.TopLeftHeaderCell.Style.BackColor = Color.Gold;
As an alternative you could also ownerdraw the cell.
Here is an example:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex < 0 && e.ColumnIndex < 0)
{
e.Graphics.FillRectangle(Brushes.Gold, e.CellBounds);
e.Handled = true;
}
..
But why would you ;-)
How to set the back color of the row / column header cell in a datagridview - c# winform
Store the current ColumnHeadersDefaultCellStyle in a variable.
Set the ColumnHeadersDefaultCellStyle to how you want the corner to be.
Then change all of the columns headers to how you want columns 0 to ... back to the old style.
Below is an example where the form is called "MyForm". This example shows the default constructor of MyForm.
Example:
public MyForm()
{
InitializeComponent();
// insert code here to add columns ...
// ...
// ...
// ...
DataGridViewCellStyle oldDefault = dgview.ColumnHeadersDefaultCellStyle.Clone();
dgview.ColumnHeadersDefaultCellStyle.BackColor = Color.Red;
foreach (DataGridViewColumn item in dgview.Columns)
{
item.HeaderCell.Style = oldDefault;
}
}
DatGridView Header Cell's Background Color
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
Change RowHeader background color on DataGridView without losing default Styles
The default style for the row header should already be defined when you build your DataGridView
. So I would use:
if ( /*I want to change this row */)
{
DataGridViewCellStyle rowStyle; // = Grid.RowHeadersDefaultCellStyle;
rowStyle = Grid.Rows[i].HeaderCell.Style;
rowStyle.BackColor = Color.Wheat;
Grid.Rows[i].HeaderCell.Style = rowStyle;
}
This way you fill your rowStyle
with the predefined style and then change only the part you want to change. See if this solves your problem.
//EDIT
As you wish to keep the other stylings of the default Windows DataGridView, you would also need to set more of the other parameters of the style. See this post.
Or try this. When initialiazing:
dataGridView1.CellPainting +=
new DataGridViewCellPaintingEventHandler (dataGridView_CellPainting);
Then create the handler function with something like:
void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
DataGridView dv = sender as DataGridView;
DataGridViewCellStyle rowStyle;// = dv.RowHeadersDefaultCellStyle;
if (e.ColumnIndex == -1)
{
e.PaintBackground(e.CellBounds, true);
e.Handled = true;
if (/*I want to change this row */)
{
rowStyle = dv.Rows[e.RowIndex].HeaderCell.Style;
rowStyle.BackColor = Color.Wheat;
dv.Rows[e.RowIndex].HeaderCell.Style = rowStyle;
using (Brush gridBrush = new SolidBrush(Color.Wheat))
{
using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Clear cell
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
//Bottom line drawing
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
// here you force paint of content
e.PaintContent(e.ClipBounds);
e.Handled = true;
}
}
}
}
}
}
This code was based on this post. You would only then need to create more paint conditions for mouseover and selected state. But this should work for you.
Remember to remove: Grid.EnableHeadersVisualStyles = false;
or force it to: Grid.EnableHeadersVisualStyles = true;
.
Winform C# Datagridview paint header
If you mean Column Header
, yes. in the CellPainting Event
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1)
{
Color c1 = Color.FromArgb(255, 54, 54, 54);
Color c2 = Color.FromArgb(255, 62, 62, 62);
Color c3 = Color.FromArgb(255, 98, 98, 98);
LinearGradientBrush br = new LinearGradientBrush(e.CellBounds, c1, c3, 90, true);
ColorBlend cb = new ColorBlend();
cb.Positions = new[] { 0, (float)0.5, 1 };
cb.Colors = new[] { c1, c2, c3 };
br.InterpolationColors = cb;
e.Graphics.FillRectangle(br, e.CellBounds);
e.PaintContent(e.ClipBounds);
e.Handled = true;
}
}
Related Topics
Check If a Property Exists in a Class
How to Have a Delegate as Attribute Parameter
Can Razor Class Library Pack Static Files (Js, CSS etc) Too
Hide Form Instead of Closing When Close Button Clicked
How to Calculate the Average Rgb Color Values of a Bitmap
Getting the Index of a Particular Item in Array
How to Create a Dictionary of Generic Types
Difference Between the Keydown Event, Keypress Event and Keyup Event in Visual Studio
Foreach Control in Form, How to Do Something to All the Textboxes in My Form
How to Stop an Animation in C#/Wpf
Performance of Linq Any VS Firstordefault != Null
Automatically Rename a File If It Already Exists in Windows Way
HTML Agility Pack - Removing Unwanted Tags Without Removing Content
Is a Reference Assignment Threadsafe
Edit Raw Pixel Data of Writeablebitmap
Will Using Linq to SQL Help Prevent SQL Injection
"The Semaphore Timeout Period Has Expired" Error for Usb Connection