• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3817
  • Last Modified:

Background Image for datagridview

Hi,
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.

Thanks
0
AKleinas
Asked:
AKleinas
  • 3
  • 2
1 Solution
 
teebonProduct ManagerCommented:
Hi AKleinas,

You will need to create a custom control in order to achieve this:
http://daysofourdev.blogspot.com/2008/09/transparent-datagridview.html
0
 
Maheshwar RSoftware DeveloperCommented:
Visit the follwing site for more info on Image Binding of Datagridview

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.backgroundimage.aspx
0
 
AKleinasAuthor Commented:
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).
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
teebonProduct ManagerCommented:
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
			Get
				Return img
			End Get
			Set
				img = value
			End Set
		End Property

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

		Protected Overloads Overrides Sub OnSizeChanged(e As EventArgs)
			MyBase.OnSizeChanged(e)
			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)
			MyBase.OnCellPainting(e)
			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
				Else
					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
				Else
					c.Style.BackColor = Color.Red
					c.Style.SelectionBackColor = Color.Red
				End If
			End If
			MyBase.OnMouseClick(e)
		End Sub
	End Class
End Namespace

Open in new window

0
 
AKleinasAuthor Commented:
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 !
 
0
 
teebonProduct ManagerCommented:
Hi Akleinas,

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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now