Visual Basic 2005-如何在DataGridView控件的保存格同时显示文字与图片

摘要:Visual Basic 2005-如何在DataGridView控件的保存格同时显示文字与图片


图表1

DataGridView 控件并没有内建任何功能来让您在同一个保存格中显示出图片与文字。解决之道,是透过 CellPaint 等事件来完成自订的绘制作业。

以下我们建立一个衍生自 DataGridViewTextBoxColumn 的使用者自订数据行类,借此于保存格内的文字旁边绘制一个图片(如图表 1 所示)。我们使用 DataGridViewCellStyle.Padding 属性来调整文字位置并覆写 Paint 方法以便绘制一个图片:

Public Class TextAndImageColumn
    Inherits DataGridViewTextBoxColumn

    Private _imageValue As Image
    Private _imageSize As Size

    Public Sub New()
        MyBase.New()
        Me.CellTemplate = New TextAndImageCell
        Me.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    End Sub

    Public Property Image() As Image
        Get
            Return Me._imageValue
        End Get
        Set(ByVal value As Image)
            Me._imageValue = value
            Me._imageSize = value.Size
            If Me.InheritedStyle IsNot Nothing Then
                Dim inheritedPadding As Padding = Me.InheritedStyle.Padding
                Me.DefaultCellStyle.Padding = New Padding( _

                  ImageSize.Width, inheritedPadding.Top, _

                  inheritedPadding.Right, inheritedPadding.Bottom)
            End If          
        End Set
    End Property

    Private ReadOnly Property TextAndImageCellTemplate() As TextAndImageCell
        Get
            Return CType(Me.CellTemplate, TextAndImageCell)
        End Get
    End Property

    Friend ReadOnly Property ImageSize() As Size
        Get
            Return ImageSize
        End Get
    End Property

    Public Overrides Function Clone() As Object
        Dim c As TextAndImageColumn = CType(MyBase.Clone, TextAndImageColumn)
        c._imageValue = Me._imageValue
        c._imageSize = Me.ImageSize
        Return c
    End Function
End Class

Public Class TextAndImageCell
    Inherits DataGridViewTextBoxCell

    Private imageValue As Image
    Private imageSize As Size

    Public Property Image() As Image
        Get
            If ((Me.OwningColumn Is Nothing) _
                      OrElse (Me.OwningTextAndImageColumn Is Nothing)) Then
                Return imageValue
            ElseIf (Me.imageValue IsNot Nothing) Then
                Return Me.imageValue
            Else
                Return Me.OwningTextAndImageColumn.Image
            End If
        End Get
        Set(ByVal value As Image)
            If Not Me.imageValue.Equals(value) Then
                Me.imageValue = value
                Me.imageSize = value.Size
                Dim inheritedPadding As Padding = Me.InheritedStyle.Padding
                Me.Style.Padding = New Padding( _
                  imageSize.Width, inheritedPadding.Top, _
                  inheritedPadding.Right, inheritedPadding.Bottom)
            End If
        End Set
    End Property

    Private ReadOnly Property OwningTextAndImageColumn() As TextAndImageColumn
        Get
            Return CType(Me.OwningColumn, TextAndImageColumn)
        End Get
    End Property

    Public Overrides Function Clone() As Object
        Dim c As TextAndImageCell = CType(MyBase.Clone, TextAndImageCell)
        c.imageValue = Me.imageValue
        c.imageSize = Me.imageSize
        Return c
    End Function

    Protected Overrides Sub Paint(ByVal graphics As Graphics, _

      ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, _

      ByVal rowIndex As Integer, _

      ByVal cellState As DataGridViewElementStates, _

      ByVal value As Object, ByVal formattedValue As Object, _

      ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, _

      ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, _

      ByVal paintParts As DataGridViewPaintParts)

      MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, _

        cellState, value, formattedValue, errorText, cellStyle, _

        advancedBorderStyle, paintParts)
        If (Not (Me.Image) Is Nothing) Then
            Dim container As System.Drawing.Drawing2D.GraphicsContainer = _

              graphics.BeginContainer
            graphics.SetClip(cellBounds)
            graphics.DrawImageUnscaled(Me.Image, cellBounds.Location)
            graphics.EndContainer(container)
        End If
    End Sub
End Class

原文:大专栏  Visual Basic 2005-如何在DataGridView控件的保存格同时显示文字与图片


猜你喜欢

转载自www.cnblogs.com/petewell/p/11526699.html