DataGridView加入Combobox自訂欄位類別後,keypress時無法更改Text內容

danny
請教各位

我在 DataGridView 加入 ComboBox自訂欄位後,原本輸入都正常

後來我在自訂的 ComboBox 的 keypress 事件內,以程式去更改 Text 內容,
結果離開此輸入格後,Text的內容又變成原來的值...........是那邊有問題 ?


環境: VS2017 + Win7 + SQL 2012

自訂類別如下:
Imports System.Windows.Forms

Public Class myDGV_ComboBoxColumn
    Inherits DataGridViewColumn

    Public Property DataSource As DataTable
    Public Property DropDownWidth As Integer = 100
    Public Property DisplayMember As String = ""
    Public Property ValueMember As String = ""

    Public Sub New()
        CellTemplate = New myDGV_ComboBoxCell()
    End Sub

    Public Overrides Property CellTemplate() As DataGridViewCell
        Get
            Return MyBase.CellTemplate
        End Get
        Set(ByVal value As DataGridViewCell)
            If Not (value Is Nothing) AndAlso Not value.GetType().IsAssignableFrom(GetType(myDGV_ComboBoxCell)) Then
                Throw New InvalidCastException("Must be a myDGV_ComboBoxCell")
            End If
            MyBase.CellTemplate = value
        End Set
    End Property
End Class

Public Class myDGV_ComboBoxCell
    Inherits DataGridViewTextBoxCell

    Public Sub New()

    End Sub


    Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)

        Dim xcolumn As myDGV_ComboBoxColumn = CType(Me.OwningColumn, myDGV_ComboBoxColumn)

        Dim xeditor As myDGV_ComboBoxEditingControl = CType(DataGridView.EditingControl, myDGV_ComboBoxEditingControl)

        xeditor.DataSource = xcolumn.DataSource
        xeditor.DisplayMember = xcolumn.DisplayMember
        xeditor.ValueMember = xcolumn.ValueMember
        xeditor.DropDownWidth = xcolumn.DropDownWidth

        If (Value Is Nothing) Then
            xeditor.Text = CType(DefaultNewRowValue, String)
        Else
            xeditor.Text = CType(Value, String)
        End If
    End Sub

    Public Overrides ReadOnly Property EditType() As Type
        Get
            Return GetType(myDGV_ComboBoxEditingControl)
        End Get
    End Property

    Public Overrides ReadOnly Property ValueType() As Type
        Get
            Return GetType(String)
        End Get
    End Property

    Public Overrides ReadOnly Property DefaultNewRowValue() As Object
        Get
            Return ""
        End Get
    End Property
End Class

Public Class myDGV_ComboBoxEditingControl
    Inherits MyCombobox
    Implements IDataGridViewEditingControl

    Private dataGridViewControl As DataGridView
    Private valueIsChanged As Boolean = False
    Private rowIndexNum As Integer

    Public Sub New()

    End Sub

    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 EditingControlDataGridView() As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
        Get
            Return dataGridViewControl
        End Get
        Set(ByVal value As DataGridView)
            dataGridViewControl = value
        End Set
    End Property

    Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
        Get
            Return Text
        End Get
        Set(ByVal value As Object)
            Text = value
        End Set
    End Property

    Public Property EditingControlRowIndex() As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
        Get
            Return rowIndexNum
        End Get
        Set(ByVal value As Integer)
            rowIndexNum = 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 Function EditingControlWantsInputKey(ByVal keyData As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey
        Return True
    End Function

    Public ReadOnly Property EditingPanelCursor() As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
        Get
            Return MyBase.Cursor
        End Get
    End Property

    Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
        Return EditingControlFormattedValue
    End Function

    Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit

    End Sub

    Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
        Get
            Return False
        End Get
    End Property

    Protected Overrides Sub OnTextChanged(ByVal e As EventArgs)
        valueIsChanged = True
        EditingControlDataGridView.NotifyCurrentCellDirty(True)
        MyBase.OnTextChanged(e)
    End Sub

    Protected Overrides Sub OnSelectedIndexChanged(ByVal e As EventArgs)
        valueIsChanged = True
        dataGridViewControl.NotifyCurrentCellDirty(True)
        MyBase.OnSelectedItemChanged(e)
    End Sub
End Class


Public Class MyCombobox  '自訂控制項 ------------------------
    Inherits ComboBox

    Public Sub New()

    End Sub

    Private Sub MyCombobox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
        Text = "123"   '以程式去更改 Text 內容後, 當離開此輸入格後,Text的內容就又變成原來的值 456
        e.Handled = True
    End Sub
End Class

danny
上面的 MyCombobox_KeyPress() 是已經簡化的程式碼,方便測試用

實際的 MyCombobox_KeyPress() 是做一些 CharacterCasing 轉大寫,及 Mask 輸入遮罩 的處理
把 MyComboBox 加到表單內,運作是正常的,但是加到 DataGridView,就不正常了

或者各位可推薦其它相關的網頁可參考
roar
Combobox先載入有可能影響column位置,最好是先載入Datagridview,比較好處理。
danny
謝謝 roar

是否有範例可供參考
回到頂部