| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3667 人关注过本帖
标题:[求助]DataGridView行验证问题
只看楼主 加入收藏
homesite
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2007-9-27
收藏
 问题点数:0 回复次数:6 
[求助]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: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:863
专家分:17
注 册:2006-11-16
收藏
得分:0 
不会吧

全国最大的 Java专业电子书免费分享[url]http:///in.asp?id=xrmao[/url]
2007-11-15 08:34
homesite
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2007-9-27
收藏
得分:0 
真的啦,你可以试一下就知道了。

我急需找解决方法,高手们快来呀!
2007-11-15 17:50
homesite
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2007-9-27
收藏
得分:0 
不会吧,大虾都没有遇到这个问题吗?

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

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

先谢谢了!
2007-11-15 18:04
homesite
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2007-9-27
收藏
得分:0 
你好,

我下载了你的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: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
解决了吗?

飘过~~
2007-11-29 09:32
homesite
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2007-9-27
收藏
得分:0 
解决了,上面的帖子就是解决方法,供大家参考!!
2007-12-03 23:04
快速回复:[求助]DataGridView行验证问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015260 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved