0

我如何使验证只通过正整数?我有一列可以触发错误,如果它是非整数,但是用户仍然可以在该列的单元格中键入负整数。

4

3 回答 3

0

Nvm 我找到了它,我稍后会更新链接列索引 = 指示您当前的单元格是否在该列中,使用以下代码验证它,对不起,麻烦的家伙。链接在这里

           If grdDataGrid.CurrentCell.ColumnIndex = 4 Then
           If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
            OrElse newInteger < 0 Then

            e.Cancel = True
            Me.grdDataGrid.Rows(e.RowIndex).ErrorText = "the value must be a non-negative         integer"
            MsgBox("You must enter a positive number")
        End If
     End If
于 2013-06-07T08:49:14.637 回答
0

在您的 Datagridview EditingControlShowing 事件中尝试...

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    Try

        If UCase(sCellName) = "QUANTITY"

             AddHandler e.Control.KeyPress, AddressOf TextNumberKeypress

        End If

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


Sub TextNumberKeypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    Dim k As Byte = Asc(e.KeyChar)

    If Not IsNumSimple(k) Then
        e.Handled = True
        If Not (k = 8 Or k = 13 Or k = 1 Or k = 3 Or k = 22) Then e.Handled = True
    End If
End Sub
于 2013-06-07T08:50:00.283 回答
0

使用javascript代码怎么样?添加这个js事件

onkeypress=""return allowOnlyNumber(event);

那么你所需要的就是

function allowOnlyNumber(evt)
{
  var charCode = (evt.which) ? evt.which : event.keyCode
  if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;
  return true;
}

否则,您可以使用 AjaxControlToolkit 中的 FilteredTextBoxExtender 作为

<asp:TextBox ID="txtbox1" runat="server"></asp:TextBox>
            <cc1:FilteredTextBoxExtender id="ftbe" runat="server" targetcontrolid="txtbox1"
                  filtertype="Numbers" validchars="0123456987" />
于 2013-06-07T07:41:14.217 回答