以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  一年内同一企业名称禁止重复输入  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=133811)

--  作者:deliangzhaoe
--  发布时间:2019/4/21 9:06:00
--  一年内同一企业名称禁止重复输入
一年内同一企业名称不能输入两次,datacolchanged设置了如下代码:
\'本年内企业名称禁止重复
If e.DataCol.Name = "企业名称" Then
    Dim filter As String = "年度 = \'" & Date.Today.year & "\'"
    If e.DataRow("企业名称") = Nothing Then
        filter &= " and 企业名称 is null"
    Else
        filter &= " and 企业名称 = \'" & e.DataRow("企业名称") & "\'"
    End If
    Dim nr As DataRow = DataTables("安全费用提取").Find(filter)
    If nr IsNot Nothing Then
        e.Cancel = True \'那么取消输入并提示用户
        Dim Result As DialogResult
        Result = MessageBox.Show("本年度本公司已添加了安全费用提取记录,禁止重复添加!    要删除当前行吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If Result = DialogResult.Yes Then
            Tables("安全费用提取").Current.Delete
        Else
        End If
    End If
End If

代码有问题:
1.企业名称不重复时,也提示重复
2.重复并删除时报错:
.NET Framework 版本:2.0.50727.5420
Foxtable 版本:2018.10.9.1
错误所在事件:表,安全费用提取,DataColChanged
详细错误信息:
调用的目标发生了异常。
此行已从表中移除并且没有任何数据。BeginEdit() 将允许在此行中创建新数据

请老师帮忙将代码修改一下,谢谢!

--  作者:有点色
--  发布时间:2019/4/21 18:07:00
--  

1、

 

Dim nr As DataRow = DataTables("安全费用提取").Find(filter)

 

改成

 

Dim nr As DataRow = DataTables("安全费用提取").Find(filter, 1)

 

2、

 

Tables("安全费用提取").Current.Delete

 

改成

 

e.datarow.Delete

 

3、尽量不要在datacolchanged事件删除行,不然如果你有代码循环给各列赋值,都会报错(你删除行了,再赋值,自然报错)


--  作者:deliangzhaoe
--  发布时间:2019/4/21 18:50:00
--  
好的,谢谢