×¢²á | µÇ¼ Íü¼ÇÃÜÂ룿 51ctoÊ×Ò³ | ²©¿Í | ÂÛ̳ | ÕÐÆ¸
ÈȵãÎÄÕ һ¸öºÚ¿ÍÓëÒ»¸öµçÄ԰׳յÄ..
¡¡°ïÖú

Visual Basic 2005¡ª¡ªÈçºÎÔÚDataGridView¿Ø¼þµÄ´¢´æ¸ñÖÐͬʱÏÔʾ³öÎÄ×ÖÓëͼƬ


2006-09-27 11:43:47
°æÈ¨ÉùÃ÷£ºÔ­´´×÷Æ·£¬ÈçÐè×ªÔØ£¬ÇëÓë×÷ÕßÁªÏµ¡£·ñÔò½«×·¾¿·¨ÂÉÔðÈΡ£
ÉÏ´ÎÓжÁÕß˵ÎÒдµÃÌ«¼òµ¥£¬ÄÇôÎÒÃǽñÌì¾Íд¸öÄÑÒ»µãµÄ°É£¡ºÜ¶àÓû§Ñ¯ÎÊ£¬ÈçºÎÈçͼ1Ëùʾ£¬ÔÚDataGridView¿Ø¼þµÄ´¢´æ¸ñÖÐͬʱÏÔʾ³öͼƬÓëÎÄ×Ö¡£
ͼ1
 
DataGridView¿Ø¼þ²¢Ã»ÓÐÄÚ½¨Èκι¦ÄÜÀ´ÈÃÄúÔÚͬһ¸ö´¢´æ¸ñÖÐÏÔʾ³öͼƬÓëÎÄ×Ö¡£½â¾öÖ®µÀ£¬ÊÇͨ¹ýCellPaintµÈʼþÀ´Íê³É×Ô¶©µÄ»æÖÆ×÷Òµ¡£
 
ÒÔÏÂÎÒÃǽ¨Á¢Ò»¸öÑÜÉú×ÔDataGridViewTextBoxColumnµÄÓû§×Ô¶©Êý¾ÝÐÐÀà±ð£¬½å´ËÓÚ´¢´æ¸ñÄÚµÄÎÄ×ÖÅԱ߻æÖÆÒ»¸öͼƬ¡£ÎÒÃÇʹÓÃ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

±¾Îijö×Ô ¡°ÕÂÁ¢Ãñ¡± ²©¿Í£¬×ªÔØÇëÓë×÷ÕßÁªÏµ£¡





    ÎÄÕÂÆÀÂÛ
 
2007-12-06 10:35:23
ÕÂÀÏʦ£¬ÄúºÃ£¡ÎÒÕâ¸öDAtagridviewÊôÐÔIntFigsû·¨ºÍNumberµÄIntFigsÁ¬½ÓÆðÀ´£¬ÇëÄú°ïÎÒ¿´Ò»¸ö£¬Ð»Ð»
Imports System
Imports System.Windows.Forms
'========================================================================================
'                   NumbericBoxµ¥ÔªÁÐ
'========================================================================================
Public Class NumericBoxColumn
  Inherits DataGridViewColumn


  Public Sub New()
    MyBase.New(New NumericBoxCell())
  End Sub

  '»ñÈ¡»òÉèÖÃÓÃÓÚ´´½¨Ðµ¥Ôª¸ñµÄÄ£°å¡£
  Public Overrides Property CellTemplate() As DataGridViewCell
    Get
        Return MyBase.CellTemplate
    End Get
    Set(ByVal value As DataGridViewCell)

        ' Ensure that the cell used for the template is a CalendarCell.
        If (value IsNot Nothing) AndAlso Not value.GetType().IsAssignableFrom(GetType(NumericBoxCell)) Then
          Throw New InvalidCastException("Must be a CalendarCell")
        End If
        MyBase.CellTemplate = value
    End Set
  End Property
  'ÄÚ²¿‰äÊý
  Private mValue As Double = 0
  Private mIntFigs As Integer = 1
  Private mDecFigs As Integer = 0
  Private mFormatString As String = "0"
  Private mMaxValue As Double = 1.7976931348623157E+308
  Private mMinValue As Double = -1.7976931348623157E+308

  Public Property IntFigs() As Integer
    Get
        Return mIntFigs
    End Get
    Set(ByVal value As Integer)
        mIntFigs = value
    End Set
  End Property

  ' Override the Clone method so that the Enabled property is copied.
  Public Overrides Function Clone() As Object
    Dim Cell As NumericBoxColumn = CType(MyBase.Clone(), NumericBoxColumn)
    Cell.IntFigs = Me.IntFigs
    Return Cell
  End Function
End Class
'========================================================================================
'                   NumbericBoxµ¥Ôª¸ñ
'========================================================================================
'ÏÔʾ DataGridView ¿Ø¼þÖеĿɱ༭Îı¾ÐÅÏ¢¡£
Public Class NumericBoxCell
  Inherits DataGridViewTextBoxCell

  Public Sub New()
    ' Use the short date format.
    'Me.Style.Format = "#"
    Me.IntFigs = 5
  End Sub

  '¸½¼Ó²¢³õʼ»¯¼ÄË޵ı༭¿Ø¼þ¡£
  Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)

    ' Set the value of the editing control to the current cell value.
    MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)

    Dim ctl As CalendarEditingControl = CType(DataGridView.EditingControl, CalendarEditingControl)
    ctl.Value = CDbl((Me.Value))
    ctl.IntFigs = Me.IntFigs
  End Sub

  '»ñÈ¡µ¥Ôª¸ñµÄ¼ÄËޱ༭¿Ø¼þµÄÀàÐÍ¡£
  Public Overrides ReadOnly Property EditType() As Type
    Get
        ' Return the type of the editing contol that CalendarCell uses.
        Return GetType(CalendarEditingControl)
    End Get
  End Property

  'ÌṩֵÀàÐ͵ĻùÀà¡£
  Public Overrides ReadOnly Property ValueType() As Type
    Get
        ' Return the type of the value that CalendarCell contains.
        Return GetType(Double)
    End Get
  End Property

  '»ñÈ¡´´½¨ÐÂÐÐʱʹÓõÄĬÈÏÖµ¡£
  Public Overrides ReadOnly Property DefaultNewRowValue() As Object
    Get
        ' Use the current date and time as the default value.
        Return 0
    End Get
  End Property
  '------------------------------------------------------------
  Private mValue As Double = 0
  Private mIntFigs As Integer = 1
  Private mDecFigs As Integer = 0
  Private mFormatString As String = "0"
  Private mMaxValue As Double = 1.7976931348623157E+308
  Private mMinValue As Double = -1.7976931348623157E+308

  Public Property IntFigs() As Integer
    Get
        Return mIntFigs
    End Get
    Set(ByVal value As Integer)
        mIntFigs = value
    End Set
  End Property
  ' Override the Clone method so that the Enabled property is copied.
  Public Overrides Function Clone() As Object
    Dim Cell As NumericBoxCell = CType(MyBase.Clone(), NumericBoxCell)
    Cell.IntFigs = Me.IntFigs
    Return Cell
  End Function

  '-------------------------------------------------
End Class

'========================================================================================
'                   NumbericBox¿Ø¼þ
'========================================================================================
Class CalendarEditingControl
  Inherits NumericBox
  Implements IDataGridViewEditingControl

  Private dataGridViewControl As DataGridView
  Private valueIsChanged As Boolean = False
  Private rowIndexNum As Integer
  '³õʼ¸ñʽ»¯Îª0
  Public Sub New()
    'Me.FormatString = "0"
  End Sub

  'ÉèÖÃDataGridView¸ñʽ»¯ºóµÄÖµ
  Public Property EditingControlFormattedValue() As Object _
    Implements IDataGridViewEditingControl.EditingControlFormattedValue

    Get
        Return Me.Text
    End Get

    Set(ByVal value As Object)
        If TypeOf value Is String Then
          Me.Value = CDbl((value))
        End If
    End Set

  End Property

  '¼ìË÷µ¥Ôª¸ñµÄ¸ñʽ»¯Öµ¡£

  Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object _
    Implements IDataGridViewEditingControl.GetEditingControlFormattedValue

    Return Me.Text

  End Function

  'ÉèÖñ³¾°,×ÖÌå
  Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) _
    Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl

    Me.Font = dataGridViewCellStyle.Font
    Me.ForeColor = dataGridViewCellStyle.ForeColor
    Me.BackColor = dataGridViewCellStyle.BackColor

  End Sub

  '»ñÈ¡»òÉèÖÃËùÊôµ¥Ôª¸ñµÄ¸¸ÐеÄË÷Òý¡£
  Public Property EditingControlRowIndex() As Integer _
    Implements IDataGridViewEditingControl.EditingControlRowIndex

    Get
        Return rowIndexNum
    End Get
    Set(ByVal value As Integer)
        rowIndexNum = value
    End Set

  End Property

  'È·¶¨Ö¸¶¨µÄ¼üÊÇÓ¦Óɱ༭¿Ø¼þ´¦ÀíµÄ³£¹æÊäÈë¼ü£¬»¹ÊÇÓ¦ÓÉ DataGridView ´¦ÀíµÄÌØÊâ¼ü¡£
  Public Function EditingControlWantsInputKey(ByVal key As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
    Implements IDataGridViewEditingControl.EditingControlWantsInputKey

    ' Let the DateTimePicker handle the keys listed.
    Select Case key And Keys.KeyCode
        Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
          Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp

          Return True

        Case Else
          Return False
    End Select

  End Function

  '×¼±¸µ±Ç°Ñ¡Öеĵ¥Ôª¸ñÒÔ½øÐб༭¡£
  Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
    Implements IDataGridViewEditingControl.PrepareEditingControlForEdit

    ' No preparation needs to be done.

  End Sub

  '»ñȡһ¸öÖµ£¬Ö¸Ê¾ÊÇ·ñÐèÒªÔÚÿ´ÎÖµ¸ü¸ÄÊ±ÖØÐ¶¨Î»µ¥Ôª¸ñÄÚÈÝ¡£
  Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean _
    Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange

    Get
        Return False
    End Get

  End Property

  '»ñÈ¡»òÉèÖðüº¬×éºÏ¿ò¿Ø¼þµÄ DataGridView
  Public Property EditingControlDataGridView() As DataGridView _
    Implements IDataGridViewEditingControl.EditingControlDataGridView

    Get
        Return dataGridViewControl
    End Get
    Set(ByVal value As DataGridView)
        dataGridViewControl = value
    End Set

  End Property

  '»ñÈ¡»òÉèÖÃÒ»¸öÖµ£¬¸Ãֵָʾ±à¼­¿Ø¼þµÄÖµÊÇ·ñÓë³ÐÔØµ¥Ôª¸ñµÄÖµ²»Í¬¡£
  Public Property EditingControlValueChanged() As Boolean _
    Implements IDataGridViewEditingControl.EditingControlValueChanged

    Get
        Return valueIsChanged
    End Get
    Set(ByVal value As Boolean)
        valueIsChanged = value
    End Set

  End Property
  'ÉèÖùâ±ê
  Public ReadOnly Property EditingControlCursor() As Cursor _
    Implements IDataGridViewEditingControl.EditingPanelCursor

    Get
        Return MyBase.Cursor
    End Get

  End Property


  'Òý·¢ÒÑʵÏÖµÄ ValueChanged ʼþ
  Public Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)
    ' Notify the DataGridView that the contents of the cell have changed.
    valueIsChanged = True
    Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
    MyBase.OnValueChanged(eventargs)
  End Sub



End Class





 

·¢±íÆÀÂÛ

êÇ   ³Æ£º
ÑéÖ¤Â룺 ¡¡µã»÷ͼƬ¿ÉË¢ÐÂÑéÖ¤Âë¡¡¡¡²©¿Í¹ý2¼¶£¬ÎÞÐèÌîдÑéÖ¤Âë
ÄÚ   ÈÝ£º