简体   繁体   English

删除Datagridview行以及我的访问数据库vb.net中的行

[英]Delete Datagridview rows as well as in my access database vb.net

this is my code for loading the data in my database going to my datagrid 这是我的代码,用于将数据库中的数据加载到datagrid

Private Sub Records_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim connString As String = "Provider=Microsoft.Ace.Oledb.12.0; Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\BackUp\Database3.Accdb;"
        Dim MyConn As OleDbConnection
        Dim da As OleDbDataAdapter
        Dim ds As DataSet
        Dim tables As DataTableCollection
        Dim source1 As New BindingSource
        MyConn = New OleDbConnection
        MyConn.ConnectionString = connString
        ds = New DataSet
        tables = ds.Tables
        da = New OleDbDataAdapter("Select * from [userinfo] ORDER BY ID", MyConn) 'Change items to your database name
        da.Fill(ds, "userinfo") 'Change items to your database name
        Dim cb = New OleDbCommandBuilder(da)
        Dim view As New DataView(tables(0))
        source1.DataSource = view
        DataGridView1.DataSource = view
    End Sub

and this is my delete button to delete a selected row in datagrid but the whole table is deleted instead (i know this cause i put it to delete the whole table just to know my sql is working) 这是我的删除按钮,用于删除datagrid中的选定行,但是整个表都被删除(我知道这是因为我知道SQL正在工作,所以我将其删除了整个表)

Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
    Dim connString As String = "Provider=Microsoft.Ace.Oledb.12.0; Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\BackUp\Database3.Accdb;"
    Dim MyConn As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim ds As DataSet
    Dim tables As DataTableCollection
    Dim source1 As New BindingSource
    Dim row As New Integer
    Try
        MyConn = New OleDbConnection
        MyConn.ConnectionString = connString
        ds = New DataSet
        tables = (ds.Tables)
        da = New OleDbDataAdapter("Delete * from [userinfo]", MyConn)
        da.Fill(ds, "userinfo")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

i want to delete a selected row in my datagrid. 我想删除数据网格中的选定行。 thank you in advance 先感谢您

Delete * from [userinfo] will delete everything in the table. Delete * from [userinfo]将删除表中的所有内容。 Pass your ID to be deleted. 传递您的ID即可将其删除。 Eg. 例如。 Delete * from [userinfo] where ID=5

You can use Datagridview.SelectedRows Property to find the selected Rows. 您可以使用Datagridview.SelectedRows属性找到选定的行。 Pass the ID Column index and you will get the ID. 传递ID列索引,您将获得ID。

Eg. 例如。 "Delete * from [userinfo] where ID=" & DataGridView1.SelectedRows(0).Cells(0).Value.ToString()

If you Allow Multi-Select, Loop through the rows to delete. 如果您允许多选,请循环浏览要删除的行。 Also use parameters to pass variables to queries for security purposes. 为了安全起见,还可以使用参数将变量传递给查询。

Sorry my bad English. 对不起,我的英语不好。 Step 1: Listen to click on a Datagrid 第1步:聆听单击Datagrid

Private Sub datagridview_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles datagridview.CellClick

Step 2: Get what column click 步骤2:点击哪一列

datagridview.Rows.Item(e.RowIndex).Cells(0).Value

Step 3: Delete from Database 步骤3:从数据库中删除

Dim sqlcmd As String = "delete  from [userinfo] where YourColumn = '" & datagridview.Rows.Item(e.RowIndex).Cells(0).Value & "'"

Fully code: 完整代码:

Private Sub Datagridview_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView.CellClick
        Try
            Dim MyConn As OleDbConnection
            Dim connString As String = "Provider=Microsoft.Ace.Oledb.12.0; Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\BackUp\Database3.Accdb;"
            MyConn = New OleDbConnection
            Dim cmdstr As String = "delete * from [userinfo] where YourColumn = '" & DataGridView.Rows.Item(e.RowIndex).Cells(0).Value & "'"
            Dim cmd As New OleDbCommand(cmdstr, MyConn)
            MyConn.Open()
            cmd.ExecuteNonQuery()
            MyConn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
 End Sub

The way to refer to deleted row values is row original value from deleted rows table: 引用删除的行值的方法是删除行表中的行原始值:

dt is datasource of datagridview
Dim dtDelete As DataTable = dt.GetChanges(DataRowState.Deleted)
cmd.Parameters.AddWithValue("@GridValue", r("Gridcolumn", DataRowVersion.Original))

the example is SQL but method is not specific 示例是SQL,但方法不是特定的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM