Dans : QSIDeveloper disait :
Fred, I tried the EditingControlWantsInputKey method but could not
get it to work. Could you post the code here so I can compare it ?
Yes, this is a very simple code that only allows string value type.
Actually my real exemple doesn't inherit a ComboBox but a UserControl so I had to cut a lot of code but this one works quite fine even if not so complete
To use this new column type you can write :
Dim cc As New DataGridViewComboColumn
cc.HeaderText = "Test"
cc.ValueType = GetType(String)
Me.DataGridView1.Columns.Add(cc)
So, the most important part now (care about the line breaks)
Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Public Class DataGridViewComboColumn
Inherits DataGridViewColumn
Sub New()
MyBase.New(New DataGridViewComboCell())
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(DataGridViewComboCell)) Then
Throw New InvalidCastException("DataGridViewComboCell expected")
End If
MyBase.CellTemplate = value
End Set
End Property
Public Overrides Property [ReadOnly]() As Boolean
Get
Return False
End Get
Set(ByVal value As Boolean)
'MyBase.[ReadOnly] = value
End Set
End Property
End Class
Public Class DataGridViewComboCell
Inherits DataGridViewComboBoxCell
Public Overrides ReadOnly Property EditType() As Type
Get
Return GetType(DataGridViewComboEditingControl)
End Get
End Property
Public Overrides ReadOnly Property FormattedValueType() As System.Type
Get
Return GetType(String)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As System.Type
Get
Return GetType(String)
End Get
End Property
Public Overrides Function KeyEntersEditMode(ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
If e.Alt And e.KeyCode = Keys.Down Then Return True
Return MyBase.KeyEntersEditMode(e)
End Function
Protected Overrides Function GetFormattedValue(ByVal value As Object, ByVal rowIndex As Integer, ByRef cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal valueTypeConverter As System.ComponentModel.TypeConverter, ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter, ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object
If value IsNot Nothing Then Return value Else Return cellStyle.NullValue
End Function
Public Overrides Function ParseFormattedValue(ByVal formattedValue As Object, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter, ByVal valueTypeConverter As System.ComponentModel.TypeConverter) As Object
Return DirectCast(formattedValue, String)
End Function
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 editingControl As DataGridViewComboEditingControl = TryCast(MyBase.DataGridView.EditingControl, DataGridViewComboEditingControl)
If (Not editingControl Is Nothing) Then
editingControl.DropDownStyle = ComboBoxStyle.DropDown
editingControl.Text = initialFormattedValue
End If
End Sub
End Class
Public Class DataGridViewComboEditingControl
Inherits ComboBox
Implements IDataGridViewEditingControl
Private _dataGridView As DataGridView
Private _rowIndex As Int32
Private _valueChanged As Boolean
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 Me._dataGridView
End Get
Set(ByVal value As System.Windows.Forms.DataGridView)
Me._dataGridView = 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
End Set
End Property
Public Property EditingControlRowIndex() As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
Get
Return Me._rowIndex
End Get
Set(ByVal value As Integer)
Me._rowIndex = value
End Set
End Property
Public Property EditingControlValueChanged() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
Get
Return Me._valueChanged
End Get
Set(ByVal value As Boolean)
Me._valueChanged = 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
If keyData = Keys.F5 Then Return True
If keyData = (Keys.Alt And Keys.Down) And Me.Enabled Then Return True
If keyData = Keys.Down And Not Me.DroppedDown Then Return False
If keyData = Keys.Up And Not Me.DroppedDown Then Return False
If keyData = Keys.Enter And Me.DroppedDown Then Return True
If keyData = Keys.Right And Not Me.DroppedDown Then Return True
If keyData = Keys.Left And Not Me.DroppedDown Then Return True
If keyData = Keys.End And Not Me.DroppedDown Then Return True
If keyData = Keys.Home And Not Me.DroppedDown Then Return True
If keyData = Keys.Escape And Me.DroppedDown Then Return True
If keyData = Keys.Down And Me.DroppedDown Then Return True
If keyData = Keys.Up And Me.DroppedDown Then Return True
Return dataGridViewWantsInputKey
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 e As System.EventArgs)
MyBase.OnTextChanged(e)
Me.EditingControlValueChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
End Sub
Protected Overrides Sub OnSelectedIndexChanged(ByVal e As System.EventArgs)
MyBase.OnSelectedIndexChanged(e)
Me.EditingControlValueChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
End Sub
End Class