如何防止datagridview vb.net中的空格 [英] How to prevent white space in datagridview vb.net

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

问题描述

我有一个使用datagridview的vb.net windows窗体应用程序。我希望找到一种方法来阻止用户输入空格或空字符串,如果他们输入无效输入。我将看到一条消息,说明他们的错误,然后将提供默认值。这是我到目前为止,它可以防止完全空白的单元格,但如果我有一个空格(即按空格键添加空白字符串),它不知道它仍然是空白输入。



 如果(columnindex =  0 然后 ' 仅检查第1列的值 
Dim cellString = DataGridView1.Rows(rowindex).Cells(columnindex).value
If cellString Nothing OrElse IsDBNull(cellString ) OrElse cellString.ToString = 字符串 .Empty 然后

MessageBox.Show( 不能为空
DataGridView1.Rows(rowindex).Cells(columnindex).value = 默认值
退出 Sub
结束 如果

解决方案

...您可以尝试删除空格并检查长度。



下面的内容应该是良好的开端。



 如果(columnindex =  0 然后 ' 仅检查第1列的值 
Dim cellString = DataGridView1.Rows(rowindex).Cells(columnindex).value
如果 cellString 没什么 OrElse IsDBNull(cellString) OrElse cellString.ToString = String 。空 OrElse cellString.Trim()。长度= 0 然后

MessageBox.Show( 不能为空
DataGridView1.Rows (rowindex)。细胞(columnindex).value = 默认值
退出 Sub
结束 如果


I have a vb.net windows form application using a datagridview. I'm hoping to find a way to prevent a user from entering whitespaces or empty strings and if they enter invalid input. I'll have a message appear explaining their mistake and thereafter a default value will be supplied. This is what I have so far and it prevents completely blank cells but if I had a whitespace (i.e. press space bar to add blank string) it doesn't know that it's still blank input.

If (columnindex = 0) Then 'checking value for column 1 only
            Dim cellString = DataGridView1.Rows(rowindex).Cells(columnindex).value
            If cellString Is Nothing OrElse IsDBNull(cellString) OrElse cellString.ToString = String.Empty Then

                MessageBox.Show("Cannot Be Empty")
                DataGridView1.Rows(rowindex).Cells(columnindex).value = "Default Value"
                Exit Sub
            End If

解决方案

... you could try removing the whitespace and checking the length.

Something like the below should be a good start.

If (columnindex = 0) Then 'checking value for column 1 only
            Dim cellString = DataGridView1.Rows(rowindex).Cells(columnindex).value
            If cellString Is Nothing OrElse IsDBNull(cellString) OrElse cellString.ToString = String.Empty OrElse cellString.Trim().Length=0 Then
 
                MessageBox.Show("Cannot Be Empty")
                DataGridView1.Rows(rowindex).Cells(columnindex).value = "Default Value"
                Exit Sub
            End If


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

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