Problem when try to delete datarow from datatable


Hi,
I have treeView with checkboxes.... When I uncheck checkbox related to treenode (if I do only for one node at the time before I save changes against database) then row deletion from datatable works OK...

But when I try to uncheck more then 1 node in treeView before I save changes against database then I get the following message:
"Deleted row information cannot be accessed through the row."

My vb code is bellow....

Please, I would appreciate a lot if you could help me to solve the problem...
Thanks in advance...

Regards...
Private Sub oTV_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles oTV.AfterCheck

        If e.Node.ForeColor = Color.Blue Then GoTo Exitline
        CheckAllChildren(e.Node, e.Node.Checked)
Exitline:
        If e.Node.Checked = False Then
            UnCheckAllChildren(e.Node, e.Node.Checked)
        End If



    End Sub

Private Sub UnCheckAllChildren(ByVal tn As TreeNode, ByVal bCheck As [Boolean])


        If tn.Nodes.Count = 0 Then
            For Each dr As DataRow In ds.Tables("dtSP2").Rows

                If dr("Leaf_value") = tn.Tag Then
                    dr.Delete()
                End If
            Next
End If
End sub

Open in new window

LVL 1
alsamAsked:
Who is Participating?
 
Kiran SonawaneConnect With a Mentor Project LeadCommented:
Sorry change the condition to
If dr.RowState <> DataRowState.Deleted Then
0
 
Kiran SonawaneProject LeadCommented:
When a row is in DataRowState.Deleted, the normal means for accessing the values do not work (as you have seen).  If all you need to know is that the row is deleted, check the RowState property.

means if row state is not deleted then dr.Delete()
0
 
alsamAuthor Commented:
How would I apply it to my solution...I have seen your comment before but anyway I don't know how to apply the same...
Could you help me to adjust my code
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Kiran SonawaneProject LeadCommented:
Replace each loop with

For Each dr As DataRow In ds.Tables("dtSP2").Rows

If dr("Leaf_value") = tn.Tag Then
    If dr.RowState = DataRowState.Deleted Then
       dr.Delete()
    End If
     dr.Delete()
End If
Next
0
 
alsamAuthor Commented:
Hi sonawanekiran,
Thanks..it works now OK ... you definetilly earned 500 points...
But following to the named problem I tried your solution to implement also when I uncheck parent treenode (which has 20 childnodes) to delete all child nodes from dt...vb below... Unfortunatelly I get the following error:

"Collection was modified; enumeration operation might not execute."

I would appreciate a lot your help again if you agree to follow with the issue...(not directly related to the original problem)

My vb code is below
Private Sub oTV_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles oTV.AfterCheck

        If e.Node.ForeColor = Color.Blue Then GoTo Exitline
        CheckAllChildren(e.Node, e.Node.Checked)
Exitline:
        If e.Node.Checked = False Then
            UnCheckAllChildren(e.Node, e.Node.Checked)
        End If



    End Sub


  Private Sub UnCheckAllChildren(ByVal tn As TreeNode, ByVal bCheck As [Boolean])
        bParentTrigger = True

        If tn.Nodes.Count = 0 Then

            For Each dr As DataRow In ds.Tables("dtSP2").Rows
                If dr.RowState <> DataRowState.Deleted Then
                    If dr("Leaf_value") = tn.Tag Then
                        dr.Delete()
                    End If
                End If
            Next
        Else
            Dim i As Integer
            For i = 0 To ds.Tables("dtSP2").Rows.Count
                For Each ctn As TreeNode In tn.Nodes
                    bChildTrigger = True
                    ctn.Checked = bCheck
                    bChildTrigger = False

                    For Each dr As DataRow In ds.Tables("dtSP2").Rows
                        If dr.RowState <> DataRowState.Deleted Then
                            If dr("Leaf_value") = tn.Tag Then
                                dr.Delete()
                            End If
                        End If
                    Next
                    CheckAllChildren(ctn, bCheck)
                Next

            Next

        End If
        bParentTrigger = False
    End Sub

Open in new window

0
 
Kiran SonawaneProject LeadCommented:
Are you trying to modify datarow anywhere?
This is genuine problem and it occurs when executing the loop on dataset/datatable rows and in the loop itself you are adding new/change rows causing modification in it which is not permissible
0
 
alsamAuthor Commented:
This are events that occur  on AfterChech event...
Just for your beter understanding I attached print screen in word doc to see how treeview initially looks after load...
Basicaly, in order to uncheck all child nodes I need to check and then uncheck parent node (3 * Service Cost Centers - L4)
This is maybe where problem begins (even I try to avoid to execute modification in datatable by checking childnode color or if the same ones are allready checked)...
Can you maybe give me reply from your side what could be wrong...I would appreciate a lot..
thanks treeView.doc
Private Sub oTV_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles oTV.AfterCheck

        If e.Node.ForeColor = Color.Blue Then GoTo Exitline
        CheckAllChildren(e.Node, e.Node.Checked)
Exitline:
        If e.Node.Checked = False Then
            UnCheckAllChildren(e.Node, e.Node.Checked)
        End If



    End Sub



    Private Sub CheckAllChildren(ByVal tn As TreeNode, ByVal bCheck As [Boolean])
        bParentTrigger = False

    
        If tn.Nodes.Count = 0 Then
            Dim newfilterRow As DataRow = ds.Tables("dtSP2").NewRow()
            newfilterRow("FILTER_NAME") = ListView1.SelectedItems.Item(0).Text.ToString
            newfilterRow("FOLDER_NAME") = Me.ToolStripComboBox1.Text.ToString
            newfilterRow("LEAF_NAME") = Me.ToolStripComboBox2.Text.ToString
            newfilterRow("LEAF_VALUE") = tn.Tag.ToString
            newfilterRow("DESCRIPTION") = tn.Name.ToString
            ds.Tables("dtSP2").Rows.Add(newfilterRow)
        Else

            For Each ctn As TreeNode In tn.Nodes

                If ctn.Checked = True Then GoTo Exitline
                bChildTrigger = False
                ctn.Checked = bCheck
                bChildTrigger = True
                If ctn.Nodes.Count = 0 Then
                    CheckAllChildren(ctn, bCheck)
                    Dim newfilterRow As DataRow = ds.Tables("dtSP2").NewRow()
                    newfilterRow("FILTER_NAME") = ListView1.SelectedItems.Item(0).Text.ToString
                    newfilterRow("FOLDER_NAME") = Me.ToolStripComboBox1.Text.ToString
                    newfilterRow("LEAF_NAME") = Me.ToolStripComboBox2.Text.ToString
                    newfilterRow("LEAF_VALUE") = ctn.Tag.ToString
                    newfilterRow("DESCRIPTION") = ctn.Name.ToString
                    ds.Tables("dtSP2").Rows.Add(newfilterRow)
                End If
Exitline:
            Next
        End If
        bParentTrigger = True

    End Sub
    Private Sub UnCheckAllChildren(ByVal tn As TreeNode, ByVal bCheck As [Boolean])
        bParentTrigger = True

        If tn.Nodes.Count = 0 Then

            For Each dr As DataRow In ds.Tables("dtSP2").Rows
                If dr.RowState <> DataRowState.Deleted Then
                    If dr("Leaf_value") = tn.Tag Then
                        dr.Delete()
                    End If
                End If
            Next
        Else
            Dim i As Integer
            For i = 0 To tn.Nodes.Count
                For Each ctn As TreeNode In tn.Nodes
                    bChildTrigger = True
                    ctn.Checked = bCheck
                    bChildTrigger = False

                    For Each dr As DataRow In ds.Tables("dtSP2").Rows
                        If dr.RowState <> DataRowState.Deleted Then
                            If dr("Leaf_value") = tn.Tag Then
                                dr.Delete()
                            End If
                        End If
                    Next
                    CheckAllChildren(ctn, bCheck)
                Next

            Next

        End If
        bParentTrigger = False
    End Sub

Open in new window

0
 
Kiran SonawaneProject LeadCommented:
I Guess
ds.Tables("dtSP2").Rows.Add(newfilterRow)
These lines causing problem in both if else condition
0
 
alsamAuthor Commented:
Sorry,
I got stuck with this problem.....
Ever since your last post I ', trying to make this working with no success...
Can you help me with some advice
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.