none
VB2008 自定义DataGridViewColumn控件的问题:如何传递属性 RRS feed

  • 问题

  • 我在参照http://msdn.microsoft.com/en-us/library/7tas5c80.aspx自己做一个DataGridViewColumn控件时碰到如下问题:
    在做(Class DataGridViewMultiColumnComboboxEditingControl)Implements IDataGridViewEditingControl的时候让它继承至另外一个UserControl(MultiColumnCombobox),这个UserControl里面有一个属性“DataTable”,这里有一个问题是,我已经可以在DataGridView的行集合里面设置这个属性,可是我如何传属性给这个UserControl?
    代码如下:
    Imports System
    
    Imports System.Windows.Forms
    
    Imports System.ComponentModel
    
    Imports System.Text
    
    Public Class DataGridViewMultiColumnCombobox
    
        Inherits DataGridViewColumn
    
        Public Sub New()
    
            MyBase.New(New DataGridViewMultiColumnComboboxCell())
    
        End Sub
    
        Public Overloads Property CellTemplete() As DataGridViewCell
    
            Get
    
                Return MyBase.CellTemplate
    
            End Get
    
            Set(ByVal value As DataGridViewCell)
    
                If (value IsNot Nothing) AndAlso _
    
                    Not value.GetType().IsAssignableFrom(GetType(DataGridViewMultiColumnComboboxCell)) _
    
                    Then
    
                    Throw New InvalidCastException("Must be a multi-column combobox cell")
    
                End If
    
                MyBase.CellTemplate = value
    
            End Set
    
        End Property
    
    
    
    #Region " Customize property "
    
        <Category("Lookup Grid")> _
    
        Public Property DataTable() As DataTable
    
            Get
    
                Return MultiColumnComboboxCellTemplate.DataTable
    
            End Get
    
            Set(ByVal value As DataTable)
    
                MultiColumnComboboxCellTemplate.DataTable = value
    
            End Set
    
        End Property
    
    
    
        Private ReadOnly Property MultiColumnComboboxCellTemplate() As DataGridViewMultiColumnComboboxCell
    
            Get
    
                Return DirectCast(CellTemplate, DataGridViewMultiColumnComboboxCell)
    
            End Get
    
        End Property
    
    
    
    #End Region
    
    
    
    End Class
    
    
    
    Public Class DataGridViewMultiColumnComboboxCell
    
        Inherits DataGridViewTextBoxCell
    
        Public Sub New()
    
            MyBase.New()
    
        End Sub
    
    
    
    #Region "Customize property"
    
        Private dataTableValue As DataTable
    
        Public Property DataTable() As DataTable
    
            Get
    
                Return dataTableValue
    
            End Get
    
            Set(ByVal value As DataTable)
    
                dataTableValue = value
    
            End Set
    
        End Property
    
    
    
        Public Overloads Overrides Function Clone() As Object
    
            Dim cell As DataGridViewMultiColumnComboboxCell = _
    
                            TryCast(MyBase.Clone(), DataGridViewMultiColumnComboboxCell)
    
            cell.DataTable = Me.DataTable
    
            Return cell
    
        End Function
    
    
    
    #End Region
    
    
    
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
    
                                                            ByVal initialFormattedValue As Object, _
    
                                                            ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
    
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
    
            Dim ctl As DataGridViewMultiColumnComboboxEditingControl = _
    
                                                            CType(DataGridView.EditingControl,  _
    
                                                            DataGridViewMultiColumnComboboxEditingControl)
    
            ctl.Text = Me.Value
    
        End Sub
    
    
    
        Public Overrides ReadOnly Property EditType() As System.Type
    
            Get
    
                Return GetType(DataGridViewMultiColumnComboboxEditingControl)
    
            End Get
    
        End Property
    
    
    
        Public Overrides ReadOnly Property ValueType() As System.Type
    
            Get
    
                Return GetType(String)
    
            End Get
    
        End Property
    
    
    
        Public Overrides ReadOnly Property DefaultNewRowValue() As Object
    
            Get
    
                Return ""
    
            End Get
    
        End Property
    
    End Class
    
    
    
    Class DataGridViewMultiColumnComboboxEditingControl
    
        Inherits MultiColumnCombobox
    
        Implements IDataGridViewEditingControl
    
    
    
        Private dataGridViewControl As DataGridView
    
        Private valueIsChanged As Boolean = False
    
        Private rowIndexNum As Integer
    
        
    
        Public Sub ApplyCellStyleToEditingControl( _
    
                            ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle) _
    
                            Implements System.Windows.Forms.IDataGridViewEditingControl.ApplyCellStyleToEditingControl
    
            Me.Font = dataGridViewCellStyle.Font
    
            Me.ForeColor = dataGridViewCellStyle.ForeColor
    
            Me.BackColor = dataGridViewCellStyle.BackColor
    
        End Sub
    
    
    
        Public Property EditingControlDataGridView() As System.Windows.Forms.DataGridView _
    
                            Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView
    
            Get
    
                Return dataGridViewControl
    
            End Get
    
            Set(ByVal value As System.Windows.Forms.DataGridView)
    
                dataGridViewControl = value
    
            End Set
    
        End Property
    
    
    
        Public Property EditingControlFormattedValue() As Object _
    
                            Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlFormattedValue
    
            Get
    
                Return Me.Text
    
            End Get
    
            Set(ByVal value As Object)
    
                Me.Text = value.ToString
    
            End Set
    
        End Property
    
    
    
        Public Property EditingControlRowIndex() As Integer _
    
                            Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
    
            Get
    
                Return rowIndexNum
    
            End Get
    
            Set(ByVal value As Integer)
    
                rowIndexNum = value
    
            End Set
    
        End Property
    
    
    
        Public Property EditingControlValueChanged() As Boolean _
    
                            Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
    
            Get
    
                Return valueIsChanged
    
            End Get
    
            Set(ByVal value As Boolean)
    
                valueIsChanged = value
    
            End Set
    
        End Property
    
    
    
        Public Function EditingControlWantsInputKey(ByVal keyData As System.Windows.Forms.Keys, _
    
                            ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
    
                            Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlWantsInputKey
    
            Select Case keyData 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 Not dataGridViewWantsInputKey
    
            End Select
    
        End Function
    
    
    
        Public ReadOnly Property EditingPanelCursor() As System.Windows.Forms.Cursor _
    
                            Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
    
            Get
    
                Return MyBase.Cursor
    
            End Get
    
        End Property
    
    
    
        Public Function GetEditingControlFormattedValue( _
    
                            ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object _
    
                            Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
    
            Return Me.Text
    
        End Function
    
    
    
        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
    
                            Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
    
    
    
        End Sub
    
    
    
        Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean _
    
                            Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
    
            Get
    
                Return False
    
            End Get
    
        End Property
    
        Protected Overrides Sub OnTextChanged(ByVal eventargs As System.EventArgs)
    
            MyBase.OnTextChanged(eventargs)
    
            NotifyDataGridViewOfValueChange()
    
        End Sub
    
        Private Sub NotifyDataGridViewOfValueChange()
    
            valueIsChanged = True
    
            dataGridViewControl.NotifyCurrentCellDirty(True)
    
        End Sub
    
    
    
    End Class
    
    
    
    <System.ComponentModel.DefaultBindingProperty("Text")> _
    
    Public Class MultiColumnCombobox : Inherits ComboBox
    
        Public Sub New()
    
    
    
        End Sub
    
        Public Overrides Property Text() As String
    
            Get
    
                Return MyBase.Text
    
            End Get
    
            Set(ByVal value As String)
    
                MyBase.Text = value
    
            End Set
    
        End Property
    
        Private dataTableValue As DataTable
    
        Public Property DataTable() As DataTable
    
            Get
    
                Return dataTableValue
    
            End Get
    
            Set(ByVal value As DataTable)
    
                dataTableValue = value
    
            End Set
    
        End Property
    
    End Class
    
    
    2009年4月3日 8:45

答案