0

我想知道我的代码有什么问题。如果我想删除,我必须勾选复选框,它允许我删除一条记录,但是当我想删除多条记录时,它不会给我一个错误,但记录没有被删除。这是我的代码

 Protected Sub chkSelect_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim chkTest As CheckBox = DirectCast(sender, CheckBox)
    Dim grdRow As GridViewRow = DirectCast(chkTest.NamingContainer, GridViewRow)

    Dim bin As TextBox = DirectCast(grdRow.FindControl("txtBin"), TextBox)
    Dim wheatGrist As TextBox = DirectCast(grdRow.FindControl("txtWheatGrist"), TextBox)
    Dim HB43S As TextBox = DirectCast(grdRow.FindControl("txtHB43S"), TextBox)
    Dim M_NIR As TextBox = DirectCast(grdRow.FindControl("txtNIR"), TextBox)
    Dim HB43 As TextBox = DirectCast(grdRow.FindControl("txtHB43"), TextBox)
    Dim WG_NIR As TextBox = DirectCast(grdRow.FindControl("txtWGNIR"), TextBox)
    Dim glutematic As TextBox = DirectCast(grdRow.FindControl("txtGlutematic"), TextBox)
    Dim handwash As TextBox = DirectCast(grdRow.FindControl("txtHandwash"), TextBox)
    Dim distilledWater As TextBox = DirectCast(grdRow.FindControl("txtDistilled"), TextBox)


    If chkTest.Checked Then
        bin.[ReadOnly] = False
        wheatGrist.[ReadOnly] = False
        HB43S.[ReadOnly] = False
        M_NIR.[ReadOnly] = False
        HB43.[ReadOnly] = False
        WG_NIR.[ReadOnly] = False
        glutematic.[ReadOnly] = False
        handwash.[ReadOnly] = False
        distilledWater.[ReadOnly] = False

        bin.ForeColor = System.Drawing.Color.Blue
        wheatGrist.ForeColor = System.Drawing.Color.Blue
        HB43S.ForeColor = System.Drawing.Color.Blue
        M_NIR.ForeColor = System.Drawing.Color.Blue
        HB43.ForeColor = System.Drawing.Color.Blue
        WG_NIR.ForeColor = System.Drawing.Color.Blue
        glutematic.ForeColor = System.Drawing.Color.Blue
        handwash.ForeColor = System.Drawing.Color.Blue
        distilledWater.ForeColor = System.Drawing.Color.Blue

    Else
        bin.[ReadOnly] = True
        wheatGrist.[ReadOnly] = True
        HB43S.[ReadOnly] = True
        M_NIR.[ReadOnly] = True
        HB43.[ReadOnly] = True
        WG_NIR.[ReadOnly] = True
        glutematic.[ReadOnly] = True
        handwash.[ReadOnly] = True
        distilledWater.[ReadOnly] = True

        bin.ForeColor = System.Drawing.Color.Black
        wheatGrist.ForeColor = System.Drawing.Color.Black
        HB43S.ForeColor = System.Drawing.Color.Black
        M_NIR.ForeColor = System.Drawing.Color.Black
        HB43.ForeColor = System.Drawing.Color.Black
        WG_NIR.ForeColor = System.Drawing.Color.Black
        glutematic.ForeColor = System.Drawing.Color.Black
        handwash.ForeColor = System.Drawing.Color.Black
        distilledWater.ForeColor = System.Drawing.Color.Black

    End If
End Sub
Private Sub UncheckAll()
    For Each row As GridViewRow In GridView1.Rows
        Dim chkUncheck As CheckBox = DirectCast(row.FindControl("chkSelect"), CheckBox)

        Dim bin As TextBox = DirectCast(row.FindControl("txtBin"), TextBox)
        Dim wheatGrist As TextBox = DirectCast(row.FindControl("txtWheatGrist"), TextBox)
        Dim HB43S As TextBox = DirectCast(row.FindControl("txtHB43S"), TextBox)
        Dim M_NIR As TextBox = DirectCast(row.FindControl("txtNIR"), TextBox)
        Dim HB43 As TextBox = DirectCast(row.FindControl("txtHB43"), TextBox)
        Dim WG_NIR As TextBox = DirectCast(row.FindControl("txtWGNIR"), TextBox)
        Dim glutematic As TextBox = DirectCast(row.FindControl("txtGlutematic"), TextBox)
        Dim handwash As TextBox = DirectCast(row.FindControl("txtHandwash"), TextBox)
        Dim distilledWater As TextBox = DirectCast(row.FindControl("txtDistilled"), TextBox)

        chkUncheck.Checked = False

        bin.ForeColor = System.Drawing.Color.Black
        wheatGrist.ForeColor = System.Drawing.Color.Black
        HB43S.ForeColor = System.Drawing.Color.Black
        M_NIR.ForeColor = System.Drawing.Color.Black
        HB43.ForeColor = System.Drawing.Color.Black
        WG_NIR.ForeColor = System.Drawing.Color.Black
        glutematic.ForeColor = System.Drawing.Color.Black
        handwash.ForeColor = System.Drawing.Color.Black
        distilledWater.ForeColor = System.Drawing.Color.Black

    Next
End Sub
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Create String Collection to store IDs of 
    'records to be deleted 
    Dim idCollection As New StringCollection()
    Dim strID As Integer

    'Loop through GridView rows to find checked rows 
    For i As Integer = 0 To GridView1.Rows.Count - 1
        Dim chkDelete As CheckBox = DirectCast(GridView1.Rows(i).Cells(0).FindControl("chkSelect"), CheckBox)

        If chkDelete IsNot Nothing Then
            If chkDelete.Checked Then
                strID = GridView1.Rows(i).Cells(1).Text
                idCollection.Add(strID)
            End If
        End If

    Next
    If idCollection.Count > 0 Then
        'Call the method to Delete records 
        DeleteRecords(idCollection)

        ' rebind the GridView
        GridView1.DataBind()
        'Else
        'lblMessage.Text = "Please select any row to delete"
    End If

End Sub

Private Sub DeleteRecords(ByVal idCollection As StringCollection)
    'Create sql Connection and Sql Command
    Dim cnnOLEDB As New OleDbConnection(strConnectionString)
    Dim cmd As New OleDbCommand
    Dim IDs As Integer

    For Each id As Integer In idCollection
        IDs += id & ","
    Next
    Try
        Dim test As Integer = IDs
        Dim sql As String = "DELETE FROM [1BK] WHERE [sampleID] in (" & test & ")"
        Dim ANS As Integer
        ANS = MsgBox("Are you sure want to delete selected record ?" & vbCrLf & vbCrLf, MsgBoxStyle.YesNo)
        If ANS = vbYes Then
            cmd.CommandType = CommandType.Text
            cmd.CommandText = sql
            cmd.Connection = cnnOLEDB
            cnnOLEDB.Open()
            cmd.ExecuteNonQuery()
        End If
        ANS = 0
    Catch ex As Exception
        Dim errorMsg As String = "Error in Deletion"
        errorMsg += ex.Message
        Throw New Exception(errorMsg)
    Finally
        cnnOLEDB.Close()
    End Try
End Sub
4

1 回答 1

0

在 deleterecords 中这样做:

Dim test as String = String.Join(",", idCollection)

而不是这个

For Each id As Integer In idCollection     
    IDs += id & ","  
Next 

然后我不完全确定这条线实际上下载了新的数据库值:

 GridView1.DataBind() 

您绝对必须再次下载您的数据库值。不只是再次将您的表格绑定到gridview。

于 2012-10-19T07:20:15.863 回答