如何使此删除按钮有效?

时间:2013-02-20 03:41:07

标签: vb.net winforms button

如何使此删除按钮有效? 公共类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作为新数据集     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

2 个答案:

答案 0 :(得分: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

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

谢谢!

答案 1 :(得分:0)

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