从数据库中删除和更新项目

时间:2013-12-11 14:20:09

标签: database vb.net

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'LoginSystemDataSet.Administrator' table. You can move, or remove it, as needed.
    Me.AdministratorTableAdapter.Fill(Me.LoginSystemDataSet.Administrator)

    With lstDetails.Columns
        .Add("Username", 200, HorizontalAlignment.Center)
        .Add("Password", 300, HorizontalAlignment.Center)
        .Add("Email", 300, HorizontalAlignment.Center)
    End With
End Sub 

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
    If lstDetails.SelectedItems.Count = 1 Then
        Dim Button = MessageBox.Show("Are you sure you want to delete this Data?", _
        "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
        If Button = Windows.Forms.DialogResult.Yes Then

            Dim dt As New LoginSystemDataSet.AdministratorDataTable
            Dim ds As New LoginSystemDataSetTableAdapters.AdministratorTableAdapter

            AdministratorBindingSource.List.Remove(lstDetails.SelectedItems.Item(0))

            AdministratorTableAdapter.Update(LoginSystemDataSet.Administrator)
            lstDetails.Items.Remove(lstDetails.SelectedItems.Item(0))

            'Dim da As New DataTable

            'AdministratorTableAdapter.DeleteQuery(lstDetails.SelectedItems.Item(0).Text)
            'lstDetails.Items.Remove(lstDetails.SelectedItems.Item(0))

            '   AdministratorTableAdapter.Update(da)
        End If
    Else
        AdministratorBindingSource.CancelEdit()
        MsgBox("No items is selected in the list provided", MsgBoxStyle.Critical)
    End If
End Sub

Private Sub AdministratorBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
    Me.Validate()
    Me.AdministratorBindingSource.EndEdit()
    Me.TableAdapterManager.UpdateAll(Me.LoginSystemDataSet)
End Sub

我正在尝试通过单击列表视图中的项目来删除行,然后从数据库中的选定行中删除它。但它不起作用。

1 个答案:

答案 0 :(得分:0)

我认为您应该使用Row.Delete,而不是仅从父级中删除一行。这里描述了不同之处: