如何使用vb.net防止datagridview中的特殊字符 [英] How to prevent special characters in datagridview using vb.net

查看:84
本文介绍了如何使用vb.net防止datagridview中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用datagridview的Windows窗体vb.net程序。我正试图找到一种方法来阻止用户在我的datagridview中输入特殊字符(例如$,@,!,#,%,^和&)。当用户输入一个特殊字符时,我会看到一个适当的消息框来解释他们的错误,然后我给他们提供了一个默认值。我有一切工作,除了一种方法来防止特殊字符或符号。我认为这样的事情必须起作用,但我似乎无法找到阻止这种进入的方法:







I have a windows forms vb.net program that uses a datagridview. I'm trying to find a way to prevent a user from entering special characters (e.g. $,@,!,#,%,^,&) in my datagridview. When the user inputs a special character I have an approprioate message box appear explaining their mistake, then I provide them a default value. I have everything working except a way to prevent the special character or symbols. I'm thinking something like this has to work, but I can't seem to find any way of preventing this sort of entry:



If (columnindex = 0) Then 'checking value for column 1 only
            Dim cellString = DataGridView1.Rows(rowindex).Cells(columnindex).value
            If cellString String.IsSymbol(cellString) = true Then

                    MessageBox.Show("Special Characters Not Allowed")
                End If
                DataGridView1.Rows(rowindex).Cells(columnindex).value = "Default Value"
                Exit Sub
            End If

推荐答案

,@,!,#,%,^,& ;)在我的datagridview中。当用户输入一个特殊字符时,我会看到一个适当的消息框来解释他们的错误,然后我给他们提供了一个默认值。我有一切工作,除了一种方法来防止特殊字符或符号。我认为这样的事情必须起作用,但我似乎无法找到阻止这种进入的方法:







,@,!,#,%,^,&) in my datagridview. When the user inputs a special character I have an approprioate message box appear explaining their mistake, then I provide them a default value. I have everything working except a way to prevent the special character or symbols. I'm thinking something like this has to work, but I can't seem to find any way of preventing this sort of entry:



If (columnindex = 0) Then 'checking value for column 1 only
            Dim cellString = DataGridView1.Rows(rowindex).Cells(columnindex).value
            If cellString String.IsSymbol(cellString) = true Then

                    MessageBox.Show("Special Characters Not Allowed")
                End If
                DataGridView1.Rows(rowindex).Cells(columnindex).value = "Default Value"
                Exit Sub
            End If


如果您希望该用户无法插入任何特殊字符,请在中处理cell_keypress 事件

如果e.key是特殊字符,则使用
if you want that user can not insert any special char then handle it in cell_keypress event
and if e.key is special char then handle it using
e.handle = true;



快乐编码!

:)


Happy Coding!
:)


这篇关于如何使用vb.net防止datagridview中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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