datagridview中的空字段 [英] empty fields in datagridview

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

问题描述

我的datagridview中有6行3列。我尝试使用以下代码检查空白字段:



 对于 r 作为 整数 =  0     5  步骤  1  
对于 c 作为 整数 = 1 3 步骤 1
如果 mydgv(c,r).Value.ToString = mydgv(c,r).Value.ToString = 0 < SP class =code-keyword>然后

MsgBox( Field不能为零或空
否则
' 执行某项操作
结束 如果
下一步 c
下一页 r





但我收到错误:

对象引用未设置为对象的实例。

有什么问题?

提前致谢。

Zlatkodo

解决方案

或者您可以使用此代码



 对于 r  As  整数 =  0     5  步骤  1  
对于 c 作为 整数 = 1 3 步骤 1
If Convert.ToString(mydgv(c,r).Value)= Convert.ToString(mydgv(c,r).Value)= 0 然后

MsgBox( 字段不能为零或为空
否则
' 做点什么
结束 如果
下一步 c
下一步 r


- 我收到多条消息(多少是空的单元格+零单元格。

- 即使使用Exit For我收到多条消息(尽可能多的是行号)

- 在某些情况下代码做某事即使有一些单元格值= 0或空。

有没有其他方法来验证单元格内容?

提前谢谢。





对于r作为整数= 0到5步骤1 
对于c作为整数= 1到3步骤1
如果转换的ToString(一个(C,R)。价值)= 或者Convert.ToString(一个(C,R)。价值)= 0,则

MSGBOX(字段不能为零或空)
'退出
其他
'做点什么

结束如果
下一步c
下一步r


There is 6 rows and 3 columns in my datagridview. I tried to check empty fields with this code:

For r As Integer = 0 To 5 Step 1
           For c As Integer = 1 To 3 Step 1
               If mydgv(c, r).Value.ToString = "" Or mydgv(c, r).Value.ToString = "0" Then

                   MsgBox("Field can not be zero or empty")
               Else
                   'do something
               End If
           Next c
       Next r



but I get an error:
Object reference not set to an instance of an object.
What is wrong?
Thanks in advance.
Zlatkodo

解决方案

or you can use this code

For r As Integer = 0 To 5 Step 1
           For c As Integer = 1 To 3 Step 1
               If Convert.ToString(mydgv(c, r).Value) = "" Or Convert.ToString(mydgv(c, r).Value) = "0" Then

                   MsgBox("Field can not be zero or empty")
               Else
                   'do something
               End If
           Next c
       Next r


- I get a multiple messages ( as many as is number of empty cells + zero cells).
- Even with Exit For I get a multiple messages (as many as is the row number)
- In some cases code "do something" even with some cells value = 0 or empty.
Is there any other way to validate cell content?
Thanks in advance.


For r As Integer = 0 To 5 Step 1
          For c As Integer = 1 To 3 Step 1
              If Convert.ToString(a(c, r).Value) = "" Or Convert.ToString(a(c, r).Value) = "0" Then
 
                  MsgBox("Field can not be zero or empty")
                  ' Exit For
              Else
                  'do something
                
              End If
          Next c
      Next r


这篇关于datagridview中的空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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