mond007
Active member
I have had a look in a number of areas but can not seem to work out the code to delete child rows when deleting the parent. See
Could it be that I have deleted the parent before the child records ? Any help would be appreciated ?
Thanks Kuldip.
VB.NET:
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
'------------------------
' DELETE PARENT ROW CODE 'MsgBox("Clicked Column is " & e.ColumnIndex)
'------------------------
If e.ColumnIndex <> 7 Then
Exit Sub
End If
Dim DeleteQuestionNo As String = DataGridView1.Rows(e.RowIndex).Cells(1).Value
If e.RowIndex <> -1 Then
If DialogResult.Yes = MessageBox.Show("Please confirm you wish to delete Question No " & DeleteQuestionNo & " ?", "Delete Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) Then
DataGridView1.Rows.RemoveAt(e.RowIndex)
' -------------------------------------
' NOW DELETE ASSOCIATED CHILD ROWS
'--------------------------------------
MsgBox("Selected Parent Row is " & DeleteQuestionNo)
Dim foundParentRow As DataRow = CareTrainQuestionAnswerData.Tables("Questions").Select(String.Format("QuestionNo ='{0}'", DeleteQuestionNo)).FirstOrDefault
If Not foundParentRow Is Nothing Then
Dim childRows As List(Of DataRow) = foundParentRow.GetChildRows("Questions_To_Answers").ToList
For Each childRow In childRows
MsgBox("Associated Child Rows are : " & UCase(childRow("AnswerRef").ToString) & " " & childRow("AnswerDescription").ToString)
DataGridView1.Rows.RemoveAt(e.RowIndex)
Next
DataGridView1.Refresh()
End If
End If
End If
End Sub
Could it be that I have deleted the parent before the child records ? Any help would be appreciated ?
Thanks Kuldip.