如何在datagridview中限制非数字输入

如何在datagridview中限制非数字输入

本文介绍了如何在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

推荐答案

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


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

08-12 05:42