1. Inherit DataGridTextBoxColumn
2. Overload/Override the Edit sub and put the logic the way you want it.
Inherited DataGridTextBoxColumn
Public Class clsGridTextBoxColumn
Inherits DataGridTextBoxColumn
#Region "Declarations"
Private WithEvents ue As TextBox = New TextBox
#End Region
#Region " Component Designer generated code "
Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()
'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
Edit - This is were you have to put the logic, you can create your on event handlers and add it your form. Here as an example I am making cell editable only if there is a value already present.
#Region "Events"
Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal read_Only As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
Dim isReadOnly As Boolean = False
Dim sValue As String
'Any cell with NULL value keep it readOnly
If (IsDBNull(Me.GetColumnValueAtRow(source, rowNum))) Then isReadOnly = True
.Visible = True
.Focus()
.Select(0, 0)
.ResumeLayout()
End With
End If
End Sub
#End Region
End Class
And in your form - create a class of the new type you just created
Dim oColStyle As clsGridTextBoxColumn = New clsGridTextBoxColumn
With oColStyle
.MappingName = "column_name"
.HeaderText = "Header Text"
.Width = 100
End With
'Add column styles to grid style
With gridTableStyle.GridColumnStyles
.Add(oColStyle)
End With
'Add grid style to the grid
DataGrid1.TableStyles.Add(gridTableStyle)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.