合同编号 产品码 …… 产品名称
202312001 202312001-01 A
202312001 202312001-02 B
……
老师,假如有一个合同,编号为202312001 , 合同里有多项产品,如何每增加一项产品时,自动生成上述产品码(合同编号 + “-” + 2未流水号)
请老师帮忙,感谢。
'产品码
Select e.DataCol.Name
Case "合同编号"
If e.DataRow.IsNull("合同编号") Then '如果合同编号为空,则产品码为空
e.DataRow("产品码") = Nothing
Else
Dim lb As String = e.DataRow("合同编号") & "-" '产产品码前缀
If e.DataRow("产品码").StartsWith(lb) = False Then '如果单据编号前缀不符
Dim max As String
Dim idx As Integer
Dim flt As String
flt = "合同编号 = '" & lb & "' And [_Identify] <> " & e.DataRow("_Identify")
max = e.DataTable.Compute("Max(产品码)", flt) '取得该类别的最大编号
If max > "" Then '如果存在最大编号
idx = CInt(max.Substring(11, 2)) + 1 '获得最大编号的后三位顺序号,并加1
Else
idx = 1 '否则顺序号等于1
End If
e.DataRow("产品码") = lb & Format(idx, "00")
End If
End If
End Select
当合同编号是:2022000027出现2次的时候,为什么产品码都是2022000027-01 ?上面代码哪里不对么
idx = CInt(max.Substring(11, 2)) + 1改为
idx = CInt(max.Substring(max.length - 2)) + 1
flt = "合同编号 = '" & lb & "' And [_Identify] <> " & e.DataRow("_Identify")改为
flt = "合同编号 = '" & e.DataRow("合同编号") & "' And [_Identify] <> " & e.DataRow("_Identify")