【问题标题】:VB.Net DataGridView will accept numeric values only in edit modeVB.Net DataGridView 将仅在编辑模式下接受数值
【发布时间】:2019-01-09 12:23:24
【问题描述】:

我的 datagridview 中的一个特定单元格将仅在编辑模式下接受数字。 我想要的是,如果他们将单元格留空或为 0,它将自动更改为值 1。

下面是我的代码:

Private Sub dgvPrint_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgvPrint.EditingControlShowing
    Dim txtEdit As TextBox = e.Control

    RemoveHandler txtEdit.KeyPress, AddressOf CopiesText_KeyPress
    AddHandler txtEdit.KeyPress, AddressOf CopiesText_KeyPress
End Sub



Private Sub CopiesText_KeyPress(sender As Object, e As KeyPressEventArgs) Handles CopiesText.KeyPress

    If dgvPrint.CurrentCell.ColumnIndex = 5 Then
        If IsNumeric(e.KeyChar.ToString()) Or e.KeyChar = ChrW(Keys.Back) Then
            e.Handled = False
        Else
            e.Handled = True
            MessageBox.Show("Invalid input." & vbCrLf & "Please enter numeric value.", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End If

End Sub

【问题讨论】:

  • 也许对于焦点丢失事件,您可以进行与您已经做过的类似的检查,您将焦点丢失,检查其是否为第 5 列并查看单元格值是否为 = 0 或 = "" 然后为该单元格赋值 1
  • CellEndEdit 似乎最有可能满足这一要求。

标签: vb.net datagridview


【解决方案1】:

感谢您的想法。我做的是

Private Sub dgvPrint_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgvPrint.CellEndEdit
    For X = 0 To dgvPrint.Rows.Count - 1
        If Val(dgvPrint.Rows(X).Cells("dgvcCopies").Value) = 0 Then
            dgvPrint.Rows(X).Cells("dgvcCopies").Value = 1
        End If
    Next
End Sub

它正在工作但我不知道这是否是正确的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    相关资源
    最近更新 更多