[VB.Net]DataGridView 某欄 限制只能輸入數字




    '限制不能輸入數字以外的字元
    Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        bt_Save.Text = DataGridView1.CurrentCell.ColumnIndex
        If DataGridView1.CurrentCell.ColumnIndex = 3 Then '欄位索引3

            Dim EditCell As DataGridViewTextBoxEditingControl
            EditCell = CType(e.Control, DataGridViewTextBoxEditingControl)
            EditCell.SelectAll()
            AddHandler EditCell.KeyPress, AddressOf Cells_KeyPress
        End If

    End Sub

    Private Sub Cells_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs)
        If e.KeyChar <> Chr(8) And e.KeyChar <> Chr(13) And (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) Then
            Beep()
            Beep()
            e.KeyChar = Chr(0)
        End If
    End Sub

留言

這個網誌中的熱門文章

[Excel]將圖片放置於儲存格中

[電腦軟體]偵測遠端電腦的網路 Port是否開啟

[軟體]AUTOCAD關於開啟圖面時,字型找不到,而要手動指定成 chineset.shx 字型檔問題