| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付赛孚耐:软件保护加密专家
身份认证令牌USB KEY   
共有 1186 人关注过本帖
标题:[求助]DataGridView行验证问题
收藏  订阅  推荐  打印 
homesite
Rank: 2
等级:注册会员
帖子:90
积分:1024
注册:2007-9-27
[求助]DataGridView行验证问题

'验证一行的“银行编号”是否为空
Private Sub DataGridView1_RowValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
If blnLoadForm Then Exit Sub '加载窗体中跳过行验证
If e.RowIndex = Me.DataGridView1.Rows.Count - 1 Then Exit Sub
If String.IsNullOrEmpty(Me.DataGridView1.Rows(e.RowIndex).Cells("银行编号").Value.ToString.Trim) Then
Me.DataGridView1.Rows(e.RowIndex).Cells("银行编号").Selected = True
Me.DataGridView1.BeginEdit(True)
Me.DataGridView1.Rows(e.RowIndex).ErrorText = "银行编号不能为空"
blnRowValidated = False
e.Cancel = True
Exit Sub
End If
blnRowValidated = True
Me.DataGridView1.Rows(e.RowIndex).ErrorText = Nothing
End Sub

问题描述如下:这段程序所在的窗体如果是以单独窗体运行的话是没有问题的,但如果是以MDI的子窗体运行的话,如果DataGridView行验证信息不能满足时单击其它控件(如Button控件)整个程序将没有响应而且CPU占用率达到90%以上,但如果单击其它单元格则没有问题。经发现如果去除e.Cancel=True语句则没有问题,但是这样的话如果单击其它单元格时行验证不能满足时又无法中止后续操作。

另外一个大问题就是在编辑一行时,如果行验证信息不能满足时就去点击列标题进行排序时,行验证时将出错。(操作无效,原因是它导致对SetCurrentCellAddressCore函数的可重入调用。)


[此贴子已经被作者于2007-11-15 17:51:31编辑过]

搜索更多相关主题的帖子: DataGridView  验证  
2007-11-14 22:02
simpson
Rank: 4
等级:高级会员
威望:6
帖子:795
积分:8088
注册:2006-11-16

不会吧
2007-11-15 08:34
homesite
Rank: 2
等级:注册会员
帖子:90
积分:1024
注册:2007-9-27

真的啦,你可以试一下就知道了。

我急需找解决方法,高手们快来呀!
2007-11-15 17:50
homesite
Rank: 2
等级:注册会员
帖子:90
积分:1024
注册:2007-9-27

不会吧,大虾都没有遇到这个问题吗?

我可不是乱讲的,你们可以试一下就知道了,我在三台机上试了又装了SP1结果还是一样,很郁闷呀!

是虫还是我技术问题,还请大虾们多多提点才是呀。

先谢谢了!
2007-11-15 18:04
homesite
Rank: 2
等级:注册会员
帖子:90
积分:1024
注册:2007-9-27

你好,

我下载了你的sample, 运行程序后确实看到了你说的问题。

我查了一下我们内部的资料,发现这是一个已知的问题。

当MDI子窗体中的DataGridView中的当前cell处于编辑状态时,如果点击另外一个按钮,DataGridView将先失去焦点,如果我们没有在CellValidating事件中取消该事件的话,那么和当前cell相关联的editing control就会被detach, 并且当前cell退出编辑状态。

这时如果我们在DataGridView的RowValidating事件中将cancel设为true, 那么DataGridView将试图恢复原先当前cell的编辑状态,但是由于和当前cell相关联的EditingControl已经被detach了,所以ContainerControl.UpdateFocusedControl()方法就会被不停的调用,从而产生了死循环,导致程序无响应。

解决的办法是设置一个标志,当DataGridView失去焦点时在CellValidating事件中验证数据。
以下是个例子:

Private leavingGrid As Boolean

    Private ReadOnly Property IsCurrentRowValid() As Boolean
        Get
            Dim currentRow As DataGridViewRow = Me.DataGridView1.CurrentRow
            If (currentRow IsNot Nothing) Then
                If (String.IsNullOrEmpty(currentRow.Cells(0).Value)) Then
                    Return False
                Else
                    Return True
                End If
            End If
        End Get
    End Property

    Private Sub DataGridView1_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
        leavingGrid = False
    End Sub


    Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
        If (leavingGrid) Then
            e.Cancel = Not IsCurrentRowValid
            If (e.Cancel) Then
                Me.DataGridView1.CurrentRow.ErrorText = "Invalid data"
            End If
        End If

    End Sub

    Private Sub DataGridView1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Leave
        leavingGrid = True
    End Sub


    Private Sub DataGridView1_RowValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
        Console.WriteLine("row validating")
        e.Cancel = Not IsCurrentRowValid
        If (e.Cancel) Then
            Me.DataGridView1.CurrentRow.ErrorText = "Invalid data"
        End If
    End Sub

希望对你有帮助。

刘婷
在线技术支持工程师
微软全球技术支持中心
2007-11-28 16:17
bygg
Rank: 12Rank: 12Rank: 12
来自:乖乖的心中
等级:版主
威望:186
帖子:10232
积分:100917
注册:2006-10-23

解决了吗?

一个天才顶不上十个笨蛋!
书山有路勤为径,学海无涯友相伴。
我的E-mail:mylover624@yahoo.com.cn
2007-11-29 09:32
homesite
Rank: 2
等级:注册会员
帖子:90
积分:1024
注册:2007-9-27

解决了,上面的帖子就是解决方法,供大家参考!!
2007-12-3 23:04
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.057623 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved