原来的代码没问题的:
Dim qsmc As WinForm.ComboBox = e.Form.Controls("棋赛名称")
Dim di1 As String = qsmc.Value
Dim m As WinForm.TextBox = e.Form.Controls("第几轮")
Dim di2 As String = m.Value
Dim bsrq As WinForm.DateTimePicker = e.Form.Controls("rq")
Dim di3 As Date = bsrq.Value
Dim dr As DataRow = DataTables("比赛积分").SQLfind("[棋赛名称] = '" & di1 & "'")
Dim a As String = Cstr(Val(di2)-1)
If DataTables("比赛积分").DataRows.count = 0 OrElse dr Is Nothing Then
Dim dts As List(of DataRow) = DataTables("基本信息").SQLSelect("[棋赛名称] = '" & di1 & "'And [身份] = '参赛人'")
Dim i2 As Integer = 1
Do While dts.Count > 0
Dim idx As Integer = rand.Next(0,dts.Count-1)
dr = DataTables("比赛积分").AddNew()
dr("桌号") = math.Ceiling((i2+1) \ 2)
dr("日期") = di3
dr("棋赛名称") = di1
dr("第几轮比赛") = di2
dr("编号") = dts(idx)("编号")
dr("姓名") = dts(idx)("姓名")
dts.RemoveAt(idx)
i2 += 1
Loop
DataTables("比赛积分").Save()
DataTables("对阵表").DataRows.Clear
Dim drs1 As List(of DataRow) = DataTables("比赛积分").SQLSelect("[棋赛名称] = '" & di1 & "'And [第几轮比赛] = '" & di2 & "'","","第几轮比赛,桌号")
For i As Integer = 0 To drs1.Count - 1 Step 2
If i+1 <= drs1.Count - 1
Dim nr As DataRow = DataTables("对阵表").AddNew()
nr("桌号") = drs1(i)("桌号")
nr("红方_编号") = drs1(i)("编号")
nr("红方_姓名") = drs1(i)("姓名")
nr("黑方_编号") = drs1(i+1)("编号")
nr("黑方_姓名") = drs1(i+1)("姓名")
End If
Next
因为还要利用dr继续写代码,就改为:
Dim qsmc As WinForm.ComboBox = e.Form.Controls("棋赛名称")
Dim di1 As String = qsmc.Value
Dim m As WinForm.TextBox = e.Form.Controls("第几轮")
Dim di2 As String = m.Value
Dim bsrq As WinForm.DateTimePicker = e.Form.Controls("rq")
Dim di3 As Date = bsrq.Value
Dim drs As List(of DataRow) = DataTables("比赛积分").SQLSelect("[棋赛名称] = '" & di1 & "'And [第几轮比赛] = '" & di2 & "'")
For Each dr As DataRow In drs
Dim a As String = Cstr(Val(di2)-1)
Dim i2 As Integer = 1
Dim dts As List(of DataRow) = DataTables("基本信息").SQLSelect("[棋赛名称] = '" & di1 & "'And [身份] = '参赛人'")
DataTables("比赛积分").DeleteFor("棋赛名称 is null and 第几轮比赛 is null")
If di2 =1 And (DataTables("比赛积分").DataRows.count = 0 OrElse drs.count <= dts.Count) Then
Do While dts.Count > 0
Dim idx As Integer = rand.Next(0,dts.Count-1)
dr = DataTables("比赛积分").AddNew
dr("桌号") = math.Ceiling((i2+1) \ 2)
dr("日期") = di3
dr("棋赛名称") = di1
dr("第几轮比赛") = di2
dr("编号") = dts(idx)("编号")
dr("姓名") = dts(idx)("姓名")
dts.RemoveAt(idx)
i2 += 1
Loop
DataTables("比赛积分").Save()
DataTables("对阵表").DataRows.Clear
Dim drs1 As List(of DataRow) = DataTables("比赛积分").SQLSelect("[棋赛名称] = '" & di1 & "'And [第几轮比赛] = '" & di2 & "'","","桌号")
For i As Integer = 0 To drs1.Count - 1 Step 2
If i+1 <= drs1.Count - 1
Dim nr As DataRow = DataTables("对阵表").AddNew
nr("桌号") = drs1(i)("桌号")
nr("红方_编号") = drs1(i)("编号")
nr("红方_姓名") = drs1(i)("姓名")
nr("黑方_编号") = drs1(i+1)("编号")
nr("黑方_姓名") = drs1(i+1)("姓名")
End If
Next
红色部分是我改动过的,删除添加的空行,不允按了多次按钮生成了多次数据。