Background Image for datagridview

Posted on 2010-01-10
Medium Priority
Last Modified: 2013-11-07
I need to add an image to the background of the whole datagridview using VB in VS2008. I would like to display the datagridview with an image in the background with all of the text in all of the cells on top of the image OR display a datgridview with a transparent background on top of a picturebox that is in back of the datagridview.

Question by:AKleinas
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 26280439
Hi AKleinas,

You will need to create a custom control in order to achieve this:

Expert Comment

by:Maheshwar R
ID: 26280706
Visit the follwing site for more info on Image Binding of Datagridview


Author Comment

ID: 26282800
Thanks, but I have looked at the daysofourdev solution and I'm not sure how to implement it in VB. I also do not need to click into the cell (the form is just displayed on a monitor). I'm sure I have to implement a custom control, but I don't have a good example of where what code goes where (in VB).
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.


Expert Comment

ID: 26284680
Hi AKleinas,

Actually the custom control code is provided as the last part, imagedgv.
I have converted the imagedgv class's codes into VB as attached below.

1. Add a new code file into your solution and paste the code into it.
2. Change the namespace testproject to the namespace of your project.
3. Replace "DataGridview" in your current code with  "imagedgv"

Note: I have not actually compiled the code as I don't have Visual Studio right now. Let me know if it doesn't compile.

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing

Namespace testproject
	Public Class imagedgv
		Inherits DataGridView
		Private img As Image
		Public Property Img() As Image
				Return img
			End Get
				img = value
			End Set
		End Property

		Public Sub New()
			SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
		End Sub

		Protected Overloads Overrides Sub OnSizeChanged(e As EventArgs)
			img = New Bitmap(img, Me.Width - Me.RowHeadersWidth, Me.Height - Me.ColumnHeadersHeight)
		End Sub

		Protected Overloads Overrides Sub PaintBackground(graphics As Graphics, clipBounds As Rectangle, gridBounds As Rectangle)
			MyBase.PaintBackground(graphics, clipBounds, gridBounds)
			graphics.DrawImage(img, New Point(Me.RowHeadersWidth, Me.ColumnHeadersHeight))
		End Sub

		Protected Overloads Overrides Sub OnCellPainting(e As DataGridViewCellPaintingEventArgs)
			If e.ColumnIndex > -1 AndAlso e.RowIndex > -1 Then
				If Me(e.ColumnIndex, e.RowIndex).Style.BackColor <> Color.Red Then
					Me(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Transparent
					Me(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Red
				End If
			End If
		End Sub

		Protected Overloads Overrides Sub OnMouseClick(e As MouseEventArgs)
			Dim hti As DataGridView.HitTestInfo = Me.HitTest(e.X, e.Y)
			If hti.Type = DataGridViewHitTestType.Cell Then
				Dim c As DataGridViewCell = Me(hti.ColumnIndex, hti.RowIndex)
				If c.Style.BackColor = Color.Red Then
					c.Style.BackColor = Color.Transparent
					c.Style.SelectionBackColor = Color.Transparent
					c.Style.BackColor = Color.Red
					c.Style.SelectionBackColor = Color.Red
				End If
			End If
		End Sub
	End Class
End Namespace

Open in new window


Author Comment

ID: 26286038
teebon: Thanks for the response, and I did just as you stated and can add the 'imagedgv' to a form, but Rob (daysofourdev) states "To get the background image, I simply created an image property for the grid and set that at design time.". I think this is what I am missing as the properties of the new control does not have a background image I can set at design time. Sorry for being so clueless on this. Thanks !

Accepted Solution

teebon earned 2000 total points
ID: 26289467
Hi Akleinas,

The image property is already there, it's the "Img".
Can you find "Img" on the designer properties window?

Featured Post

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question