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??

This is the class i've used over and over again borrowing your code thanx anyway, to use it simply call forceNumeric(datagridview1,"textboxdg","0,3") meaning column 0 and 3. By the way
Public Sub forceNumeric(ByRef obj As Object, Optional ByVal objForm As String = "textBox", Optional ByVal whichColumn As String = "0")
If objForm.ToLower = "textbox" Then
Dim textbox As TextBox = obj
AddHandler textbox.KeyDown, AddressOf NumericChecker
ElseIf objForm.ToLower = "combobox" Then
Dim combobox As ComboBox = obj
AddHandler combobox.KeyDown, AddressOf NumericChecker
ElseIf objForm.ToLower = "textboxdg" Then
Dim dg As DataGridView = obj
AddHandler dg.EditingControlShowing, AddressOf dgNumericChecker
Me.dg = dg
Me.whichcolumn = whichColumn
End If

End Sub
Private Sub dgNumericChecker(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)
Dim splitWhichColumn As String()
splitWhichColumn = whichcolumn.Split(",")
For i As Integer = 0 To splitWhichColumn.Length - 1
If dg.CurrentCell.ColumnIndex = CInt(splitWhichColumn(i)) And Not e.Control Is Nothing Then
Dim tb As TextBox = CType(e.Control, TextBox)
AddHandler tb.KeyPress, AddressOf TextBox_KeyPress

End If
Next

End Sub
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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.