繁体   English   中英

如何通过删除按钮“ BtnDelete”从datagridview删除Excel工作表数据库中的一行数据

[英]How to delete the data of a row in the excel sheet DB from datagridview through a Delete button “BtnDelete”

我不知道如何从DataGridView删除Excel工作表数据库中的行数据。 它没有显示任何错误,但是在DataGridView中选中的行也没有被删除。 不知道我的代码出了什么问题

这是我的代码:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TxtExamtime.Format = DateTimePickerFormat.Custom
        TxtExamtime.CustomFormat = "hh:MM tt"
        cn.ConnectionString = "provider=microsoft.jet.oledb.4.0; Data Source=C:\psave\New folder\save.xls;Extended Properties=Excel 8.0;"
        cn.Open()
        FillDataGridView("select ID, Family Name, Given Name, Gender, DOB, Exam Date, Exam Time, Street Name, House Nr, PLZ, City from [edit$]")

End Sub

Private Sub FillDataGridView(ByVal Query As String)
    da = New OleDbDataAdapter(Query, cn)
    dt.Clear()
    da.Fill(dt)
    With DataGridView1
        .DataSource = dt
        .Columns(0).HeaderText = "ID"
        .Columns(1).HeaderText = "Family Name"
        .Columns(2).HeaderText = "Given Name"
        .Columns(3).HeaderText = "Gender"
        .Columns(4).HeaderText = "DOB"
        .Columns(5).HeaderText = "Exam Date"
        .Columns(6).HeaderText = "Exam Time"
        .Columns(7).HeaderText = "Street Name"
        .Columns(8).HeaderText = "House Nr"
        .Columns(9).HeaderText = "PLZ"
        .Columns(10).HeaderText = "City"
        .Columns(10).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    End With
End Sub

Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
    Try
        With cm
            .Connection = cn
            .CommandText = "insert into [edit$]values('" & TxtId.Text & "','" & TxtFamilyname.Text & "','" & TxtGivenname.Text & "','" & TxtGender.Text & "','" & TxtDob.Text & "','" & TxtExamdate.Text & "','" & TxtExamtime.Text & "','" & TxtStreet.Text & "','" & TxtHouse.Text & "','" & TxtPlz.Text & "','" & TxtCity.Text & "' )"
            .ExecuteNonQueryAsync()
        End With
        FillDataGridView("select * from [edit$]")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
        Return
    End Try
    MsgBox("succefully Saved!", MsgBoxStyle.Information, Text)
End Sub

Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click
    Try
        With cm
            .Connection = cn
            .CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where id ='" & TxtId.Text & "' and Given Name = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and Exam Date'" & TxtExamdate.Text & "'and Exam Time = '" & TxtExamtime.Text & "'and Street Name = '" & TxtStreet.Text & "'and House Nr = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
            .ExecuteNonQueryAsync()
        End With
        FillDataGridView("select * from [edit$]")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Information, Text)
        Return
    End Try
    MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text)
End Sub

Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click


Try
            With cm
                .Connection = cn
                .CommandText = "Delete [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where id ='" & TxtId.Text & "' and Given Name = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and Exam Date'" & TxtExamdate.Text & "'and Exam Time = '" & TxtExamtime.Text & "'and Street Name = '" & TxtStreet.Text & "'and House Nr = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
                .ExecuteNonQueryAsync()
            End With
            FillDataGridView("select * from [edit$]")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, Text)
            Return
        End Try
        MsgBox("Succesfully Deleted!", MsgBoxStyle.Information, Text)
    End Sub

您的DELETE语句是错误的。 尝试删除记录时,应引发异常(根据您的代码显示错误的MsgBox)。

正确的语法是

DELETE FROM table_name
WHERE some_column=some_value;

例如:

DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders';

假设您要删除与所有文本框匹配的记录(按照您的代码),则CommandText应该是这样的

.CommandText = "Delete from [edit$] where [Family Name] = '" & TxtFamilyname.Text & "' and id ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"

编辑

您的消息也成功Succesfully Deleted! Catch之外。 即使抛出异常,它也会显示该消息

尝试这个

Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click

Try
            With cm
                .Connection = cn
                .CommandText = "Delete from [edit$] where [Family Name] = '" & TxtFamilyname.Text & "' and id ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
                .ExecuteNonQuery()                    
            End With
            MsgBox("Succesfully Deleted!", MsgBoxStyle.Information, Text)
            FillDataGridView("select * from [edit$]")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, Text)

End Try

End Sub

暂无
暂无

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

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