代码改成这样测试
If e.DataCol.Name = "申请日期" Then
If e.DataRow.IsNull("申请日期") Then
e.DataRow("采购申请号") = Nothing
Else
e.datarow.save
Dim dr As DataRow = e.DataTable.SQLFind("申请日期 = '" & e.DataRow("申请日期") & "' and [_Identify] <> " & e.DataRow("_Identify"))
If dr IsNot Nothing Then
e.DataRow("采购申请号") = dr("采购申请号")
Else
Dim bh As String = Format(e.DataRow("申请日期"),"yyMMDD") '取得编号的8位前缀
If e.DataRow("采购申请号").StartsWith(bh) = False '如果编号的前8位不符
Dim dt2 As DataTable = DataTables("采购申请明细")
Dim max As String
Dim idx As Integer
max = e.DataTable.SQLCompute("Max(采购申请号)","申请日期 = '" & e.DataRow("申请日期") & "' And [_Identify] <> " & e.DataRow("_Identify")) '取得该天的最大编号
msgbox(max)
If max > "" Then '如果存在最大编号
idx = CInt(max.Substring(10,3)) + 1 '获得最大编号的后三位顺序号,并加1
Else
idx = 1 '否则顺序号等于1
End If
msgbox(idx)
e.DataRow("采购申请号") = "CGSQ"& bh & Format(idx,"000")
e.DataRow.save
End If
End If
End If
End If