VB.net 2010 - datagridview - usercontrol in datagridviewcolumn
Here I have given sample code for including usercontrols in datagridview. You have to create a datagridviewcolumn class for your user control. I use this code for receiving only numeric input in the datagridviewcolumn. You can get the "TBnumber" usercontrol class in my other thread. But you have to create another class with "Implements IDataGridViewEditingControl" and inherit the tbnumber.
Learn VB.net 2010 - datagridview - usercontrol in datagridviewcolumn
You can add usercontrols to the datagridview object.
For that you have to make a new datagridviewcolumn class (with datagridview..(textbox)..cell)
The datagridview..cell should return property edittype value as your usercontrol class.
Once you create a customised DataGridViewColumn it appears at the designtime
I add the customised dgvcolumn at runtime. "It works fine."
In the following example tbnumber is my user control class.
Note: Your control class should have "Implements IDataGridViewEditingControl
" and some codes for values,events and keystrokes as given under. (You need not change this code)
Public Class NumericDGVColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.new(New NDC)
End Sub
End Class
Public Class NDC
Inherits DataGridViewTextBoxCell
Public Overrides ReadOnly Property EditType() As System.Type
Get
Return GetType(NumericCellTextBox)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As System.Type
Get
Return GetType(String)
End Get
End Property
End Class
Class NumericCellTextBox
'this is the new class to get your usercontrol into the datagridcolumn class
'here your have to chane only the first line i.e inherits tbnumber to inherit 'your user control. Remianing all codes are for datagridview to track the keys 'and values. you need not change anything
Inherits TBNumber
' (this usercontrol is for getting numeric input only)
' You can get this code for this class in my other thread
' here after you need not change any code
Implements IDataGridViewEditingControl
Private _dataGridView As DataGridView
Private _rowIndex As Integer
Private _valueChanged As Boolean
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) _
Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.BackColor = dataGridViewCellStyle.BackColor
Me.Font = dataGridViewCellStyle.Font
Me.ForeColor = dataGridViewCellStyle.ForeColor
End Sub
Public Property EditingControlDataGridView() As DataGridView _
Implements IDataGridViewEditingControl.EditingControlDataGridView
Get
Return _dataGridView
End Get
Set(ByVal value As DataGridView)
_dataGridView = 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.ToString()
End Set
End Property
Public Property EditingControlRowIndex() As Integer _
Implements IDataGridViewEditingControl.EditingControlRowIndex
Get
Return _rowIndex
End Get
Set(ByVal value As Integer)
_rowIndex = value
End Set
End Property
Public Property EditingControlValueChanged() As Boolean _
Implements IDataGridViewEditingControl.EditingControlValueChanged
Get
Return _valueChanged
End Get
Set(ByVal value As Boolean)
_valueChanged = 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 Text
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 System.EventArgs)
MyBase.OnTextChanged(e)
_valueChanged = True
_dataGridView.NotifyCurrentCellDirty(True)
End Sub
End Class