我试图通过按钮选择所有行,或者使用ctrl + click选择不同的行。 但即使我选择了多行,它也只检查第一行。然后它再次重新读取它而不是去下一行。 我不确定在哪里放置多选代码。
最初,您只需要点击一行,然后检查条件是否满足。但是,如果满足条件,则要求您可以执行多个并检查每一行。
这是我的代码:
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
Dim i As Integer
i = DataGridView1.CurrentRow.Index
_subjectno = DataGridView1.Item(0, i).Value
_title = DataGridView1.Item(1, i).Value
_unit = DataGridView1.Item(2, i).Value
_pre = DataGridView1.Item(3, i).Value
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.MultiSelect = True
DataGridView1.SelectAll()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim selectedItems As DataGridViewSelectedRowCollection = DataGridView1.SelectedRows
For Each selectedItem As DataGridViewRow In selectedItems
add()
Next
End Sub
Sub add()
Try
If IS_EMPTY(txtSno) = True Then Return
cm = New MySqlCommand("select * from tblenroll where subjectnumber like '" & _subjectno & "' and remarks <> 'Failed' and studentno like '" & txtSno.Text & "'", cn)
dr = cm.ExecuteReader
dr.Read()
If dr.HasRows Then
MsgBox("Subject is already taken.", vbExclamation)
dr.Close()
Return
Else
dr.Close()
End If
If _pre = "NONE" Or _pre = "2ND YR STANDING" Or _pre = "3RD YR STANDING" Or _pre = "4TH YR STANDING" Or _pre = "5TH YR STANDING" Then
cm = New MySqlCommand("insert into tblenroll (studentno, subjectnumber, ay,semester,dateenrolled, curriculum) values ('" & txtSno.Text & "','" & _subjectno & "','" & txtAY.Text & "','" & txtSem.Text & "','" & Now & "','" & txtCurriculum.Text & "')", cn)
cm.ExecuteNonQuery()
MsgBox(_subjectno & " successfully added.", vbInformation)
LoadEnrolled()
Else
cm = New MySqlCommand("select * from tblenroll where studentno like '" & txtSno.Text & "' and subjectnumber like '" & _subjectno & "' and ay like '" & txtAY.Text & "'", cn)
dr = cm.ExecuteReader
dr.Read()
If dr.HasRows Then
MsgBox("Subject is already taken.", vbExclamation)
dr.Close()
Return
Else
dr.Close()
End If
Dim strok As Boolean = False
Dim strArr() As String
Dim count As Integer
Dim strpre As String = _pre
strArr = strpre.Split(", ")
For count = 0 To strArr.Length - 1
cm = New MySqlCommand("select * from tblenroll as e inner join tblsubject as s on e.subjectnumber = s.subjectno where s.subjectno like '%" & Trim(strArr(count)) & "%' and studentno like '" & txtSno.Text & "' and remarks like 'Passed'", cn)
dr = cm.ExecuteReader
dr.Read()
If dr.HasRows Then
dr.Close()
strok = True
Else
MsgBox("Unable to enroll this subject. Pre-requisite " & strArr(count) & ".", vbExclamation)
dr.Close()
Return
End If
Next
If strok = True Then
cm = New MySqlCommand("insert into tblenroll (studentno, subjectnumber, ay, semester, dateenrolled, curriculum) values ('" & txtSno.Text & "','" & _subjectno & "','" & txtAY.Text & "','" & txtSem.Text & "','" & Now & "','" & txtCurriculum.Text & "')", cn)
cm.ExecuteNonQuery()
MsgBox(_subjectno & " successfully added.", vbInformation)
LoadEnrolled()
End If
End If
Catch ex As Exception
MsgBox(ex.Message, vbCritical)
End Try
End Sub
非常感谢你!
答案 0 :(得分:0)
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
For Each selectedItem As DataGridViewRow In DataGridView1.SelectedRows
_subjectno = selectedItem.Cells(0).Value
_title = selectedItem.Cells(1).Value
_unit = selectedItem.Cells(2).Value
_pre = selectedItem.Cells(3).Value
add()
Next
End Sub