none
如何可以删除datagridview的chekcbox选择的数据? RRS feed

答案

  • 你好

    或者你可以嘗試使用

    "ExecuteNonQuery"

    E.G.

    嘗試改變你的CODE 為

     For i As Integer = 0 To deletedtbase.RowCount - 1
          If (deletedtbase.Rows(i).Cells("Column1").Value) Then
            Dim ID As String = deletedtbase.Rows(i).Cells("ID").Value
            Dim objCon As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\taipan.accdb")
    
            Dim objComm As New OleDbCommand()
    
            objComm.CommandText = "DELETE FROM deldata where deletedata='" & (ID) & "'"
            MsgBox(objComm.CommandText)
            objComm.Connection = objCon
            MsgBox(objComm.CommandText)
            <strong> con.Open()
    		objComm.ExecuteNonQuery() '這可以執行這個DELETE動作
            con.Close()</strong>
          End If
        Next
    
    


    Please correct me if my concept is wrong


    Chi
    2011年7月2日 12:42
    版主

全部回复

  • 你好
    或者你可以嘗試以下方法
    假設你的 Application 上有一個DataGridView 名叫DataGridView1
    入面有一個 Boolean Field 叫"IsSelected" 以Checkbox 顯示的
     For i As Integer = 0 To DataGridView1.RowCount - 1
        If (DataGridView1.Rows(i).Cells("IsSelected").Value) Then
         '執行另一個動作去刪除資料
         E.G. Stored Procedure.當然你要自行用一些方法去找這個Record 的 ID
         E.G. 假設你的資料上有一個Field叫 Record_ID 是 auto ID 的
         Dim ID as Integer = DataGridView1.Rows(i).Cells("Record_ID")
         '執行你的SQL Code 去 delete 和這個ID 有關的record
        End
     Next
     
     Please correct me if my concept is wrong

    Chi
    2011年6月30日 13:58
    版主
  • 你好
    或者你可以嘗試以下方法
    假設你的 Application 上有一個DataGridView 名叫DataGridView1
    入面有一個 Boolean Field 叫"IsSelected" 以Checkbox 顯示的
     For i As Integer = 0 To DataGridView1.RowCount - 1
        If (DataGridView1.Rows(i).Cells("IsSelected").Value) Then
         '執行另一個動作去刪除資料
         E.G. Stored Procedure.當然你要自行用一些方法去找這個Record 的 ID
         E.G. 假設你的資料上有一個Field叫 Record_ID 是 auto ID 的
         Dim ID as Integer = DataGridView1.Rows(i).Cells("Record_ID")
         '執行你的SQL Code 去 delete 和這個ID 有關的record
        End
     Next
     
     Please correct me if my concept is wrong

    Chi


     For i As Integer = 0 To deletedtbase.RowCount - 1
                If (deletedtbase.Rows(i).Cells("Column1").Value) Then
                    Dim ID As String = deletedtbase.Rows(i).Cells("ID").Value
                    Dim objCon As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\taipan.accdb")

                    Dim objComm As New OleDbCommand()

                    objComm.CommandText = "DELETE FROM deldata  where deletedata='" & (ID) & "'"
                    MsgBox(objComm.CommandText)
                    objComm.Connection = objCon
                    MsgBox(objComm.CommandText)
                    Dim adapter As New OleDbDataAdapter(objComm)
                    con.Open()
                    Dim staffinfo As New DataTable
                    adapter.Fill(staffinfo)
                    deletedtbase.DataSource = staffinfo

                    con.Close()
                End If
            Next

     

    上面的代码有什么错误?可以连接数据库,但不可以一次过删除选择的数据。

    2011年7月2日 9:19
  • 你好

    或者你可以嘗試使用

    "ExecuteNonQuery"

    E.G.

    嘗試改變你的CODE 為

     For i As Integer = 0 To deletedtbase.RowCount - 1
          If (deletedtbase.Rows(i).Cells("Column1").Value) Then
            Dim ID As String = deletedtbase.Rows(i).Cells("ID").Value
            Dim objCon As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\taipan.accdb")
    
            Dim objComm As New OleDbCommand()
    
            objComm.CommandText = "DELETE FROM deldata where deletedata='" & (ID) & "'"
            MsgBox(objComm.CommandText)
            objComm.Connection = objCon
            MsgBox(objComm.CommandText)
            <strong> con.Open()
    		objComm.ExecuteNonQuery() '這可以執行這個DELETE動作
            con.Close()</strong>
          End If
        Next
    
    


    Please correct me if my concept is wrong


    Chi
    2011年7月2日 12:42
    版主