Ci sono diversi modi, uno dei quali (il più sbrigativo) è quello di sfruttare l'evento EditingControlShowing del DataGridView.
Ad esempio:
codice:
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCellAddress.X = 1 OrElse DataGridView1.CurrentCellAddress.X = 3 Then
AddHandler e.Control.KeyPress, AddressOf cellTextBox_KeyPress
Else
RemoveHandler e.Control.KeyPress, AddressOf cellTextBox_KeyPress
End If
End Sub
Private Sub cellTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If Not (Char.IsDigit(e.KeyChar) OrElse Char.IsControl(e.KeyChar)) Then
e.Handled = True
End If
End Sub
Nell'esempio ho impostato che le celle della colonna 1 e 3 accettano solo numeri.