Solved

Problem when try to delete datarow from datatable

Posted on 2011-09-27
9
266 Views
Last Modified: 2012-05-12

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

0
Comment
Question by:alsam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36715050
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
 
LVL 1

Author Comment

by:alsam
ID: 36715107
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
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36715138
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 17

Accepted Solution

by:
sonawanekiran earned 500 total points
ID: 36715143
Sorry change the condition to
If dr.RowState <> DataRowState.Deleted Then
0
 
LVL 1

Author Comment

by:alsam
ID: 36715537
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
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36715567
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
 
LVL 1

Author Comment

by:alsam
ID: 36715751
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
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36715777
I Guess
ds.Tables("dtSP2").Rows.Add(newfilterRow)
These lines causing problem in both if else condition
0
 
LVL 1

Author Comment

by:alsam
ID: 36716367
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question