Hi.
How can I extends the drawing of the column headers of the DataGridView
for instance, to create a GradientBrush on the background.
I've found the DataGridViewColumnHeaderCell and I'm guessing it is the key, both how to use it on the grid
Hi.
How can I extends the drawing of the column headers of the DataGridView
for instance, to create a GradientBrush on the background.
I've found the DataGridViewColumnHeaderCell and I'm guessing it is the key, both how to use it on the grid
Custom Paint of DataGridView column headers
ruerue
PaulPT
jjcosgrove
That is where you need to use the Paint* methods on the CellPaintingEventArgs class to layer your painting. Combine this with different PaintParts values and you can layer your custom painting anyway you want. Here is a simple example of painting a custom background color (put this in the CellPainting event handler):
When you run this you'll see that only the content is painted. Content equates to the header text and the sort glyph. These equate to ContentForeground and ContentBackground paint parts also. (check out the DGV faq for more info on PaintParts and what paints what for more info). What isn't painted are borders. A better example requires you to specify what you want to paint using the PaintParts bits and using the Paint method like so (again this is in the CellPainting event handler):
Since PaintParts is a bit enum (or Flag enum) I "NOT" the background bit and then "AND" that value. This removes the Background bit from All paint parts which is passed into the Paint method. You can also OR together individual paint parts.
if (e.RowIndex == -1)
{
e.Graphics.FillRectangle(Brushes.Blue, e.CellBounds);
e.PaintContent(e.ClipBounds);
e.Handled = true;
}
if (e.RowIndex == -1)
{
e.Graphics.FillRectangle(Brushes.Blue, e.CellBounds);
e.Paint(e.ClipBounds, (DataGridViewPaintParts.All & ~DataGridViewPaintParts.Background));
e.Handled = true;
}
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"
*Rick*
You can handle the CellPainting event and for the RowIndex of -1 the paint event relates to the ColumnHeader cell. You can also derive a new class from DataGridViewColumnHeaderCell and override the Paint method.
If you create your own class, then you can either a) set the column's HeaderCell property to an instance of your custom header class (you'll might want to copy some of the properties such as Text from the header cell that currently exists before overwriting the value). You can also create a custom column class that sets the HeaderCell to your custom cell in the constructor.
If you want to do this for all columns, handle the ColumnAdded event so you know when you need to set your custom header cell.
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"