如何在datagridview中限制非数字输入 [英] How to restrict non-numeric input in datagridview

查看:215
本文介绍了如何在datagridview中限制非数字输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望找到一种方法来限制用户输入我的datagridviewcolumn中的任何非数字输入。此外,我已经限制用户输入任何负数并将单元格留空。如果有人能找到限制用户输入字母和非数字输入的方法,我将非常感谢!



I'm hoping to find out a way to restrict a user from entering any non-numeric input into my datagridviewcolumn. Also I have already restricted the user from entering any negative numbers and from leaving the cell blank. If anyone can find a way to restrict the user from entering letters and non-numeric input I would greatly appreciate it!

If (e.ColumnIndex = 8) Then 'This specifies the column number
        Dim cellData = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
        If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
            MessageBox.Show("Cannot Be Empty")
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0
        ElseIf cellData < 0 Then
            MessageBox.Show("Negatives Values Not Allowed")
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0

            Exit Sub

        End If
    End If

推荐答案

尝试使用正则表达式。

我认为如果列中的值有效,像这样的函数会给你一个真/假

它只允许数字。



Try regular expression.
I think a function like this would work to give you a true/false if the value in the column is valid
It will only allows numbers.

private static bool IsNumeric(string cellData)
        {
            System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("^[0-9]*");
            return rx.IsMatch(cellData);
        }


这篇关于如何在datagridview中限制非数字输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆