Dim chkDel As CheckBox
Dim lblID As Label
Dim i As Integer
For i = 0 To dgvBrand.Items.Count - 1
chkDel = dgvBrand.Items(i).FindControl("chkDel")
lblID = dgvBrand.Items(i).FindControl("lblCustomerID")
If chkDel.Checked = True Then
strSQL = "DELETE FROM customer WHERE CustomerID = '" & lblID.Text & "' "
commDelete = New OleDbCommand(strSQL, Conn)
commDelete.ExecuteNonQuery()
End If
Next
Code ตัวนั้นมันใช้กับ ASP.NET นี่ครับ ลองดูตัวนี้ครับ
Code (VB.NET)
Private Sub toolStripLabel1_Click(sender As Object, e As EventArgs)
con = New SqlConnection(strCon)
con.Open()
Dim com As New SqlCommand()
For i As Integer = 0 To dataGridView1.Rows.Count - 2
If dataGridView1.Rows(i).Cells(0).Value IsNot Nothing Then
Dim id As [String] = dataGridView1.Rows(i).Cells(1).Value.ToString()
sql = "delete from usernamepassword where id='" & id & "'"
com.Connection = con
com.CommandType = CommandType.Text
com.CommandText = sql
com.ExecuteNonQuery()
End If
Next
End Sub
For i As Integer = dataGridView1.Rows.Count - 1 To 0 Step -1
If CBool(dataGridView1.Rows(i).Cells(0).FormattedValue) Then
dataGridView1.Rows.RemoveAt(i)
End If
Next