SIGN UP MEMBER LOGIN:    
ARTICLE

Add Image on DatagridView Cell Header

Posted by Ankur Gupta Articles | Windows Forms C# May 26, 2010
This tip shows how to add images to a Windows Forms DataGridView control cell header using VB.NET.
Reader Level:
Download Files:
 

Windows Forms DataGridView control does not have any property by which we can add an Image on the DataGrid View Header.

But we can add Image on DataGridView programetically and for this purpose we can use CellPainting event of DataGridView control.

Let's say, you have an image that you would like to draw on a DataGridView header.

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.

Note: In this code, you will replace C:\AddEmoticons04247.gif with with the full path and file name of your image file.

Dim InfoIcon As Image = Image.FromFile("C:\AddEmoticons04247.gif")

After that, we simply need to draw the image using Graphics.DrawImage metho 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.


If e.RowIndex = -1 AndAlso e.ColumnIndex = MyDataGridView.Columns.Count - 1 Then

                e.Paint(e.CellBounds, DataGridViewPaintParts.All And Not DataGridViewPaintParts.ContentForeground)

                e.Graphics.DrawImage(InfoIcon, e.CellBounds)

                e.Handled = True

            End If


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


ImageOnGridHeader.JPG 

Login to add your contents and source code to this article
Article Extensions
Contents added by Ankur Gupta on May 26, 2010
share this article :
post comment
 

Your Welcome

Posted by Ankur Gupta Apr 01, 2011

Thanku sir

Posted by surender bhyan Apr 01, 2011

really helpful

thanks

Posted by Hirendra Sisodiya Oct 22, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor