Hi, i have set validating cell control into the datagridview by only allow numeric and a single dot, but i only want to validate specific column only instead all columns.
below is what i did
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
'source: http://www.devx.com/dotnet/Article/33748/1954?pf=true
'---restrict inputs
If DataGridView1.CurrentCell.ColumnIndex = 1 And Not e.Control Is Nothing Then
Dim tb As TextBox = CType(e.Control, TextBox)
'---add an event handler to the TextBox control---
AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
AddHandler tb.TextChanged, AddressOf Textbox_TextChanged
End If
End Sub
set only numeric and integer
Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If Char.IsDigit(e.KeyChar) Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46 Then
If Asc(e.KeyChar) = 46 Then
If CType(sender, TextBox).Text.Contains(Chr(46)) Then
e.Handled = True
Else
e.Handled = False
End If
Else
e.Handled = False
End If
Else
e.Handled = True
End If
End Sub
set if more than 100
Private Sub Textbox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If (DataGridView1.Columns(1).HeaderText = "Percentage") Or (DataGridView1.Columns(1).HeaderText = "Value") Then
If CDec(CType(sender, TextBox).Text.Trim = ".") Then
MsgBox(". has to appear after an interger", MsgBoxStyle.Critical)
CType(sender, TextBox).Text = ""
CType(sender, TextBox).Focus()
End If
End If
If (DataGridView1.Columns(1).HeaderText = "Percentage") Then
If Not CType(sender, TextBox).Text.Trim.Length = 0 Then
If CDec(CType(sender, TextBox).Text >= 100) Then
MsgBox("Percentage cannot exceeds 100", MsgBoxStyle.Critical)
CType(sender, TextBox).Text = ""
End If
End If
End If
End Sub
all this apply to all columns.. anyway to limit to specific or single column only??