将特定列从Datagridview设置为仅在vb.net中输入数字

时间:2013-09-11 02:25:34

标签: vb.net datagridview

这是我的代码,用于将所有列设置为仅输入数字:

Private Sub dvBelt_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dvBelt.EditingControlShowing
    Try
        RemoveHandler e.Control.KeyPress, AddressOf TextNumberKeypress
        AddHandler e.Control.KeyPress, AddressOf TextNumberKeypress

    Catch ex As Exception
        '... 
    End Try
End Sub

Sub TextNumberKeypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

    If Asc(e.KeyChar) >= 33 And Asc(e.KeyChar) <= 47 Or _
        Asc(e.KeyChar) >= 58 Then
        e.Handled = True
    End If

End Sub

现在我想要的是只将第一列设置为仅允许输入数字,其余列可以输入字符串。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

仅当当前单元格指向第一列并将其注册到特定单元格时,dvBelt_EditingControlShowing方法内部才会注册该事件。在C ++ / Cli中,代码看起来像这样

void dvBelt_EditingControlShowing(System::Object ^sender, System::Windows::Forms::DataGridViewEditingControlShowingEventArgs ^e) {
try {

    if (this->dvBelt->CurrentCell->ColumnIndex == 0) { // 0 is the column index for the first column
        //do the removing and adding of your kepress event here
    }
} catch (Exception ^ex) {

}}