Dim i As Integer
Dim ChBox As CheckBox
For i = 0 To DataGrid1.Items.Count - 1
ChBox = CType(DataGrid1.Items(i).Cells(0).FindControl( "CheckBox1 "), CheckBox)
If ChBox.Checked = True Then
Dim conn As New SqlConnection
conn.ConnectionString = "server=127.0.0.1;uid=sa;pwd=;database=netexam "
conn.Open()
Dim sql As String
sql = "delete from question where id=ChBox "
Dim cmd As New SqlCommand(sql, conn)
cmd.ExecuteNonQuery()
bind()
End If
Next
------解决方案--------------------------------------------------------
因为你没有在循环结束就调用了Bind()方法, 如果你选择的不是第一行, 下面的CheckBox的选择全都没有效了, 你只需把Bind()的调用放在For循环后就OK了, 如下。
Dim i As Integer
Dim ChBox As CheckBox
Dim label1 As Label
For i = 0 To DataGrid1.Items.Count - 1
ChBox = CType(DataGrid1.Items(i).Cells(0).FindControl( "CheckBox1 "), CheckBox)
label1 = CType(DataGrid1.Items(1).FindControl( "label1 "), Label)
If ChBox.Checked = True Then
Dim conn As New SqlConnection
conn.ConnectionString = "server=127.0.0.1;uid=sa;pwd=;database=netexam "
conn.Open()
Dim sql As String
sql = "delete from question where id= ' " + label1.Text + " ' "
Dim cmd As New SqlCommand(sql, conn)
cmd.ExecuteNonQuery()
End If
Next
bind()