locked
how to Delete Parent and Child Rows from DataSet RRS feed

  • Question

  • I'm Using 2 DataGridViews in VB2012

    1: The Parent is for Account Information
    2: The Child is for Master Key Information

    When: I delete "Mike" from the Parent datagrid, it is Removed ... and So is the Master Key Information for "Mike"
    From Both DataGridViews

    BUT: If I re-add "Mike" all the Master Key Information Comes back
    Without adding it New

    I want to delete The Parent Record and ALL Related Master Key Records Related to it

    The Account Information Deletes Correctly and is Gone Until I add it back in
    BUT: when I re-add "Mike" back ALL the old Master Keys Information are still there

    Private Sub btnAccountDelete_Click(sender As Object, e As EventArgs) Handles btnAccountDelete.Click
    
            If SelIndex = -1 Then
    
                MsgBox("You Must Selete an Account to Delete")
    
                Exit Sub
    
                Me.Validate()
    
                Me.AccountsBindingSource.EndEdit()
                Me.MasterKeysBindingSource.EndEdit()
    
            End If
    
            If AccountsDataGridView.RowCount > 0 Then
    
                AccountsDataGridView.Rows.Remove(AccountsDataGridView.SelectedRows(SelIndex))
    
                AccountsBindingNavigatorSaveItem.PerformClick()
    
                If MasterKeysDataGridView.RowCount > 0 Then
    
                    For i = MasterKeysDataGridView.RowCount - 1 To 0 Step -1
    
                        If Not MasterKeysDataGridView.Rows(i).IsNewRow Then
                            MasterKeysDataGridView.Rows.RemoveAt(i)
                        End If
    
                    Next
    
                End If
            End If
    
            Try
                Me.Validate()
    
                Me.AccountsBindingSource.EndEdit()
                Me.MasterKeysBindingSource.EndEdit()
    
                Me.AccountsTableAdapter.Update(AccountsDataSet)
                Me.MasterKeysTableAdapter.Update(Me.AccountsDataSet)
    
                'MsgBox("Saved Worked")
    
            Catch ex As Exception
    
                MsgBox("Save Failed", , "Dlete Accoumts")
    
            End Try
    
            AccountsBindingNavigatorSaveItem.PerformClick()
    
            ReLoad_Accounts()
    
            btnSetting(1)
    
        End Sub

    Thanks

    Mike


    • Edited by mmetcalf Sunday, September 6, 2015 3:32 PM
    Sunday, September 6, 2015 3:14 PM

Answers

  • Go to the DataSet designer and select the relation between Accounts and Masterkey.  Edit the relation and enable cascading delete.  By enabling constraints and setting the delete action to cascade the dataset will delete all child records when the parent record is deleted.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, September 6, 2015 5:25 PM

All replies

  • Go to the DataSet designer and select the relation between Accounts and Masterkey.  Edit the relation and enable cascading delete.  By enabling constraints and setting the delete action to cascade the dataset will delete all child records when the parent record is deleted.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, September 6, 2015 5:25 PM
  • Which One Do I set ?



    • Edited by mmetcalf Wednesday, September 9, 2015 3:13 PM
    Wednesday, September 9, 2015 2:58 PM
  • The first option.  You want both the relation and the foreign key constraint.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, September 9, 2015 3:14 PM