2013-09-11 29 views
0

這是我設置的所有列只輸入數字代碼:從DataGridView設置特定的列輸入數字僅在vb.net

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

裏面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) { 

}} 
+0

感謝您的想法先生,我現在有一個工作代碼:) – Matthew

相關問題