功能:一.可以对不允许为空的字段进行判断,判断范围为整个datatable,通过判断返回值true,二、可以对datatable中,设置查询条件的值,进行重复的行判断。通过判断返回值为true,用法说明:
Dim dt As DataTable = DataTables(e.Form.name & "_Table1") '这里是被验证的表名.
Dim 空值 As String = dt.Name & ":空值|项目ID,部门ID,会计期间"
Dim 重复 As String = dt.Name & ":重复|项目ID,部门ID,会计期间"
If Functions.Execute("保存验证",空值) = True And Functions.Execute("保存验证",重复) = True Then
dt.Save()
MessageBox.Show("保存成功!")
End If
说明:可以在按钮事件中调用,如果现在表的BeforeSaveDataRow中调用,可以把下面的for each 循环给去掉。增加一个参数当前保存的行,就可以啦。
'以下是自定义函数内容:
'Functions.Execute("保存验证",,"表名:空值|列集合:重复|重复列")
'其中列集合用逗号分开
'表名和空值,重复用:隔开
Dim xx() As String = args(0).split(":")
If xx.Length <> 2 Then
Functions.Execute("msg","每次只能分别验证空值或者重复.")
Return False '返回假代表不执行.
End If
Dim tn As String = xx(0) '表名
Dim kk() As String = xx(1).Split("|") '分解选项
If kk.Length <> 2 Then
Functions.Execute("msg","空值或者重复和列集合或者重复列之间必须用|隔开.")
Return False
End If
Dim 选项 As String = kk(0)
Dim lm() As String = kk(1).Split(",") '列集合
If lm.Length = 0 Then
Functions.Execute("msg","必须设置验证空列集合或者验证重复列,用逗号隔开.")
Return False
End If
Dim tb As Table = Tables(tn)
Dim searchwhere As String
Dim drs As List(of DataRow)
Select Case 选项
Case "空值" '对空值验证设置.
For Each dr As DataRow In tb.DataTable.DataRows
If dr.RowState = DataRowState.Added Or dr.RowState = DataRowState.Modified Then
For Each l As String In lm '循环执行列
If dr.IsNull(l) Then '判断是否为空
Functions.Execute("msg","必须填写空值或者重复.")
Return False
End If
Next
End If
Next
Return True '返回可以往下进行的值
Case "重复"
For Each dm As DataRow In tb.DataTable.DataRows
If dm.RowState = DataRowState.Added Then
'首先查询当前表格中有没有重复的行.只查询新增的行,是否超过两个.
searchwhere = ""
For Each m As String In lm '循环执行列
searchwhere = searchwhere & m & "='" & dm(m) & "' AND "
Next
If searchwhere.Length > 5 Then
If searchwhere.SubString(searchwhere.Length-5,5) = " AND " Then
searchwhere = searchwhere.Remove(searchwhere.Length-5,5)
End If
End If
tb.Filter = searchwhere
If tb.rows.Count > 1 Then
Functions.Execute("msg","重复数据请修改" & searchwhere)
Return False
End If
drs = tb.DataTable.SQLSelect(searchwhere)
If drs.Count = 1 Then '远程查询如果存在那么直接结束循环
tb.Filter = searchwhere
Functions.Execute("msg","重复数据请修改" & searchwhere)
Return False
End If
End If
Next
tb.Filter = ""
Return True
Case Else
Functions.Execute("msg","必须填写空值或者重复.")
Return False
End Select