Solved

Problem when try to delete datarow from datatable

Posted on 2011-09-27
9
258 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
  • 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
 
LVL 17

Accepted Solution

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

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now