Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

DataGridView Custom Headers in VB.NET

, 9 Mar 2009 CPOL
How to give Custom Headers to your DataGridView
DGVHeaderColor.zip
DGVHeaderColor
DGVHeaderColor
DGVHeaderColor.exe
DGVHeaderColor.pdb
DGVHeaderColor.vbproj.user
DGVHeaderColor.vshost.exe
My Project
Application.myapp
Settings.settings
Resources
AquaBall Blue.png
Button Gray Stripe 01 050.png
DGVHeaderColor.suo
Docs
Gradient ColumnHeader_files
4BBCD906-6DAD-4443-8F0B-525208645B30.jpg
add.gif
AddTo_Blink.png
AddTo_Delicious.png
AddTo_Digg.png
AddTo_Facebook.png
AddTo_Furl.png
AddTo_Google.png
AddTo_Live.png
AddTo_MrWong.png
AddTo_Newsvine.png
AddTo_Reddit.png
AddTo_Simpy.png
AddTo_stumbleupon.png
AddTo_Technorati.png
AddTo_Yahoo.png
ajax-loader.gif
BE.gif
discuss.gif
forum_faq.gif
forum_newmsg.gif
forum_search.gif
GradientColumnheader.png
javaduke.jpg
LAMP.GIF
link.png
logo225x90.gif
mail.gif
member_sm.gif
MonthlyComp.png
msg_admin.gif
msg_answer.gif
msg_general.gif
msg_joke.gif
msg_news.gif
msg_question.gif
msg_rant.gif
pollcol.gif
print.gif
red.gif
report.gif
ServeHTML_data
developer_express_60Free-Controls-150x80.swf
ServeHTML_data_002
B3424356_data
crystal-160x600.swf
ServeHTML_data_003
Dundas_468x60_Chart.swf
ServeLinks_data
smartclient24.jpg
smiley_confused.gif
smiley_laugh.gif
smiley_OMG.gif
smiley_smile.gif
smiley_wink.gif
t.gif
thumbs_down.gif
thumbs_up.gif
t_002.gif
watchon.png
white.gif
Public Class frmMain
#Region " Constants "
	Private Enum DGVHeaderImageAlignments As Int32
		[Default] = 0
		FillCell = 1
		SingleCentered = 2
		SingleLeft = 3
		SingleRight = 4
		Stretch = [Default]
		Tile = 5
	End Enum
#End Region
#Region " Events "
#Region " Handlers "
	Private Sub dgvData_CellPainting(ByVal sender As Object, _
	ByVal e As DataGridViewCellPaintingEventArgs) _
	Handles dgvData.CellPainting
		' Only the Header Row (which Index is -1) is to be affected.
		If e.RowIndex = -1 Then
			GridDrawCustomHeaderColumns(dgvData, e, _
			 My.Resources.Button_Gray_Stripe_01_050, _
			 DGVHeaderImageAlignments.Stretch)
			'GridDrawCustomHeaderColumns(dgvData, e, _
			' My.Resources.AquaBall_Blue, DGVHeaderImageAlignments.FillCell)
			'GridDrawCustomHeaderColumns(dgvData, e, _
			' My.Resources.AquaBall_Blue, _
			' DGVHeaderImageAlignments.SingleCentered)
			'GridDrawCustomHeaderColumns(dgvData, e, _
			' My.Resources.AquaBall_Blue, DGVHeaderImageAlignments.SingleLeft)
			'GridDrawCustomHeaderColumns(dgvData, e, _
			' My.Resources.AquaBall_Blue, DGVHeaderImageAlignments.SingleRight)
			'GridDrawCustomHeaderColumns(dgvData, e, _
			' My.Resources.AquaBall_Blue, DGVHeaderImageAlignments.Tile)
		End If
	End Sub
	Private Sub frmMain_Load(ByVal sender As Object, ByVal e As EventArgs) _
	 Handles Me.Load
		' Init.
		' Set up the Header Color and Font.
		With dgvData.ColumnHeadersDefaultCellStyle
			.Alignment = DataGridViewContentAlignment.TopRight
			.BackColor = Color.DarkRed
			.ForeColor = Color.Gold
			.Font = New Font(.Font.FontFamily, .Font.Size, _
			 .Font.Style Or FontStyle.Bold, GraphicsUnit.Point)
		End With
		' Fill in some Text.
		For i As Int32 = 0 To 20
			Dim arrStrings As String()
			arrStrings = New String() _
			 { _
			 i.ToString, "Text " & i.ToString, i.ToString, _
			 "Text " & i.ToString, i.ToString _
			 }
			dgvData.Rows.Add(arrStrings)
			arrStrings = Nothing
		Next i
	End Sub
#End Region
#End Region
#Region " Methods "
	Private Sub GridDrawCustomHeaderColumns(ByVal dgv As DataGridView, _
	 ByVal e As DataGridViewCellPaintingEventArgs, ByVal img As Image, _
	 ByVal Style As DGVHeaderImageAlignments)
		' All of the graphical Processing is done here.
		Dim gr As Graphics = e.Graphics
		' Fill the BackGround with the BackGroud Color of Headers.
		' This step is necessary, for transparent images, or what's behind
		' would be painted instead.
		gr.FillRectangle( _
		 New SolidBrush(dgv.ColumnHeadersDefaultCellStyle.BackColor), _
		 e.CellBounds)
		If img IsNot Nothing Then
			Select Case Style
				Case DGVHeaderImageAlignments.FillCell
					gr.DrawImage( _
					 img, e.CellBounds.X, e.CellBounds.Y, _
					 e.CellBounds.Width, e.CellBounds.Height)
				Case DGVHeaderImageAlignments.SingleCentered
					gr.DrawImage(img, _
					 ((e.CellBounds.Width - img.Width) \ 2) + e.CellBounds.X, _
					 ((e.CellBounds.Height - img.Height) \ 2) + e.CellBounds.Y, _
					 img.Width, img.Height)
				Case DGVHeaderImageAlignments.SingleLeft
					gr.DrawImage(img, e.CellBounds.X, _
					 ((e.CellBounds.Height - img.Height) \ 2) + e.CellBounds.Y, _
					 img.Width, img.Height)
				Case DGVHeaderImageAlignments.SingleRight
					gr.DrawImage(img, _
					 (e.CellBounds.Width - img.Width) + e.CellBounds.X, _
					 ((e.CellBounds.Height - img.Height) \ 2) + e.CellBounds.Y, _
					 img.Width, img.Height)
				Case DGVHeaderImageAlignments.Tile
					' ********************************************************
					' To correct: It sould display just a stripe of images,
					' long as the whole header, but centered in the header's
					' height.
					' This code WON'T WORK.
					' Any one got any better solution?
					'Dim rect As New Rectangle(e.CellBounds.X, _
					' ((e.CellBounds.Height - img.Height) \ 2), _
					' e.ClipBounds.Width, _
					' ((e.CellBounds.Height \ 2 + img.Height \ 2)))
					'Dim br As New TextureBrush(img, Drawing2D.WrapMode.Tile, _
					' rect)
					' ********************************************************
					' This one works... but poorly (the image is repeated
					' vertically, too).
					Dim br As New TextureBrush(img, Drawing2D.WrapMode.Tile)
					gr.FillRectangle(br, e.ClipBounds)
				Case Else
					gr.DrawImage( _
					 img, e.CellBounds.X, e.CellBounds.Y, _
					 e.ClipBounds.Width, e.CellBounds.Height)
			End Select
		End If
		'e.PaintContent(e.CellBounds)
		If e.Value Is Nothing Then
			e.Handled = True
			Return
		End If
		Using sf As New StringFormat
			With sf
				Select Case dgv.ColumnHeadersDefaultCellStyle.Alignment
					Case DataGridViewContentAlignment.BottomCenter
						.Alignment = StringAlignment.Center
						.LineAlignment = StringAlignment.Far
					Case DataGridViewContentAlignment.BottomLeft
						.Alignment = StringAlignment.Near
						.LineAlignment = StringAlignment.Far
					Case DataGridViewContentAlignment.BottomRight
						.Alignment = StringAlignment.Far
						.LineAlignment = StringAlignment.Far
					Case DataGridViewContentAlignment.MiddleCenter
						.Alignment = StringAlignment.Center
						.LineAlignment = StringAlignment.Center
					Case DataGridViewContentAlignment.MiddleLeft
						.Alignment = StringAlignment.Near
						.LineAlignment = StringAlignment.Center
					Case DataGridViewContentAlignment.MiddleRight
						.Alignment = StringAlignment.Far
						.LineAlignment = StringAlignment.Center
					Case DataGridViewContentAlignment.TopCenter
						.Alignment = StringAlignment.Center
						.LineAlignment = StringAlignment.Near
					Case DataGridViewContentAlignment.TopLeft
						.Alignment = StringAlignment.Near
						.LineAlignment = StringAlignment.Near
					Case DataGridViewContentAlignment.TopRight
						.Alignment = StringAlignment.Far
						.LineAlignment = StringAlignment.Near
				End Select
				' This part could be handled...
				'Select Case dgv.ColumnHeadersDefaultCellStyle.WrapMode
				'	Case DataGridViewTriState.False
				'		.FormatFlags = StringFormatFlags.NoWrap
				'	Case DataGridViewTriState.NotSet
				'		.FormatFlags = StringFormatFlags.NoWrap
				'	Case DataGridViewTriState.True
				'		.FormatFlags = StringFormatFlags.FitBlackBox
				'End Select
				.HotkeyPrefix = Drawing.Text.HotkeyPrefix.None
				.Trimming = StringTrimming.None
			End With
			With dgv.ColumnHeadersDefaultCellStyle
				gr.DrawString(e.Value.ToString, .Font, _
				 New SolidBrush(.ForeColor), e.CellBounds, sf)
			End With
		End Using
		e.Handled = True
	End Sub
#End Region
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Luca Crisi, MCP
Software Developer (Senior)
Italy Italy
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150123.1 | Last Updated 9 Mar 2009
Article Copyright 2009 by Luca Crisi, MCP
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid