0

如何使此删除按钮起作用?公共类 Form1 Dim inc As Integer Dim MaxRows As Integer Dim dbProvider As String Dim dbSource As String Dim con As New OleDb.OleDbConnection Dim ds As New DataSet Dim da As OleDb.OleDbDataAdapter Dim sql As String

Private Sub GroupBox1_Enter(sender As System.Object, e As System.EventArgs) Handles GroupBox1.Enter

    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Data Source = C:Desktop\AddressBook\AddressBook.mdb"

    con.ConnectionString = dbProvider & dbSource

    con.Open() 

    sql = "SELECT * From Master_Details"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "Master_Details")

    MaxRows = ds.Tables("Master_Details").Rows.Count
    inc = -1

    MsgBox("Database is now open")
    con.Close()
    MsgBox("Database is now closed")

    txtcode.Text = ds.Tables("Master_Details").Rows(0).Item(0)
    txtdprt.Text = ds.Tables("Master_Details").Rows(0).Item(1)
    txtgroup.Text = ds.Tables("Master_Details").Rows(0).Item(2)
    txtepf.Text = ds.Tables("Master_Details").Rows(0).Item(3)
    txtdesignation.Text = ds.Tables("Master_Details").Rows(0).Item(4)
    txtename.Text = ds.Tables("Master_Details").Rows(0).Item(5)
    txtjdate.Text = ds.Tables("Master_Details").Rows(0).Item(6)
    txtdob.Text = ds.Tables("Master_Details").Rows(0).Item(7)
    txtnicn.Text = ds.Tables("Master_Details").Rows(0).Item(8)
    txtgender.Text = ds.Tables("Master_Details").Rows(0).Item(9)
    txttcodes.Text = ds.Tables("Master_Details").Rows(0).Item(10)
    Return
End Sub

Private Sub btndelete_Click(sender As System.Object, e As System.EventArgs) Handles btndelete.Click
    If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No 
        MsgBox("Operation Cancelled")
        Exit Sub
    End If

    Dim cb As New OleDb.OleDbCommandBuilder(da)

    ds.Tables("Master_Details").Rows(ds.Tables("Master_Details").Rows.Count).Delete()
    MaxRows = MaxRows - 1
    inc = 0
    da.Update(ds, "Master_Details")
    NavigateRecords()
End Sub
4

2 回答 2

0

您没有正确指定要删除的行 - 它始终是 row ds.Tables("Master_Details").Rows.Count,它是最后一行之后的一个。

于 2013-02-20T03:56:38.190 回答
0

用例语句...将所有这些放在您的删除按钮单击事件中...

  Select Case MsgBox("Do you really want to delete this record?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "CONFIRM DELETE")
        Case MsgBoxResult.Yes
            'NOT SURE IF THIS IS RIGHT, BUT THE SELECT IS IN WHICH YOU WANT...'
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            ds.Tables("Master_Details").Rows(ds.Tables("Master_Details").Rows.Count - 1).Delete()
            MaxRows = MaxRows - 1
            inc = 0
            da.Update(ds, "Master_Details")
            NavigateRecords()
        Case MsgBoxResult.No
             MsgBox ("Operation Cancelled")
           Exit Sub
    End Select

如果消息框结果为“是”,请在第一行设置一个断点并单步执行您的代码,这将显示正在发生的事情......

谢谢!

于 2013-02-20T03:53:41.017 回答