[VB. NET, C #]-custom controls (including source code)-Add a background image to the datagridview

Source: Internet
Author: User

Source: http://www.codeproject.com/KB/grid/Datagridview_BackImage.aspx

VB. NET

  Imports  System. componentmodel
Imports System. Drawing

Public Class Userdatagridview
Inherits Windows. Forms. datagridview
Private M_image As Image

Public Sub New ()
' Me. m_image = drawing. image. fromfile ("C: \ bg.jpg ")
End sub

Protected Overrides Sub Paintbackground ( Byval Graphics As Graphics, Byval Clipbounds As Rectangle, Byval Gridbounds As Rectangle)
Mybase . Paintbackground (graphics, clipbounds, gridbounds)
Graphics. drawimage ( Me . M_image, gridbounds)
End sub

' Add image attributes
Public Property Backimage () As Drawing. Image
Get
Return Me . M_image
End Get
Set ( Byval Value As Drawing. Image)
Me . M_image = Value
Mybase . Refresh ()
End Set
End Property
End Class

 

 

C #

  Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. LINQ;
Using System. text;
Using System. Windows. forms;

Namespace Userdatagridview
{
Public Partial Class Userdatagridview: datagridview
{
Private Image m_image;

Public Userdatagridview ()
{
Initializecomponent ();
// This. m_image = image. fromfile ("C: \ bg.jpg ");
}

/// <Summary> Override paintbackground </Summary>
Protected Override Void Paintbackground (Graphics graphics, rectangle clipbounds, rectangle gridbounds)
{
Base . Paintbackground (graphics, clipbounds, gridbounds );
Graphics. drawimage ( This . M_image, gridbounds );
}

/// <Summary> Set background image </Summary>
Public Image backimage
{
Get
{
Return This . M_image;
}
Set
{
This . M_image = Value;
Base . Refresh (); // Reload
}
}
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.