Add Image on DatagridView column header in vb.net

This article describe how we can add image to windows forms DataGridView Column header in vb.net

Add image on data grid view column header in vb.net

We know that there is no any direct property of Windows Forms DataGridView control by which we can add an Image on the DataGridView Header.
But we can add Image on DataGridView programetically and for this purpose we can use CellPainting event of DataGridView control.

Following Code sample requires one datagridview control with CellPainting event handler named ‘DataGridView1_CellPainting’.

First of all, we need to create an Image object from the image file. The following code creates an Image object from a GIF file. The FromFile static method of Image class creates an Image object from a file.

Dim IconImg As Image = Image.FromFile("C:\DgvIcon.gif")

After that, we simply need to draw the image using Graphics.DrawImage method on the CellPainting event handler. Go to your DataGridView control events and double click on CellPainting method in Properties Window and write th following code.

    Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, _
            ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) _
            Handles DataGridView1.CellPainting
        If e.RowIndex = -1 AndAlso e.ColumnIndex = DataGridView1.Columns.Count - 1 Then
            e.Paint(e.CellBounds, DataGridViewPaintParts.All And Not DataGridViewPaintParts.ContentForeground)
            e.Graphics.DrawImage(IconImg, e.CellBounds)
            e.Handled = True
        End If
    End Sub

Now if you run the application, you will see image appears on the header of the DataGridView control.

Author: Ankur

Have worked primarily in the domain of Calling, CRM and direct advertisers services. My technological forte is Microsoft Technologies especially Dot Net (Visual Studio 2003, 2005, 2008, 2010 and 2012) and Microsoft SQL Server 2000,2005 and 2008 R2. My Area of Expertise is in C#. Net, VB.Net, MS-SQL Server, ASP. Net, Silverlight, HTML, XML, Crystal Report, Active Reports, Infragistics, Component Art, ComponeOne, Lead Tools etc.