Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27034

Circular References within a DataTable...Help please!

$
0
0
Hi,

I have two datatables with 2 columns each. One is items available and the other is items assigned.

Code:

        Dim dtAvailable As New DataTable
        Dim dtAssigned As New DataTable

        dtAssigned.Columns.Add("TEST_ID", GetType(Int32))
        dtAssigned.Columns.Add("ASSIGNED_TEST_ID", GetType(Int32))
        dtAvailable.Columns.Add("TEST_ID", GetType(Int32))
        dtAvailable.Columns.Add("AVAILABLE_TEST_ID", GetType(Int32))

        Dim dr As DataRow
        'Row One
        dr = dtAvailable.NewRow()
        dr("TEST_ID") = 1
        dr("AVAILABLE_TEST_ID") = 2
        dtAvailable.Rows.Add(dr)
        dr = Nothing

        'Row Two
        dr = dtAvailable.NewRow()
        dr("TEST_ID") = 1
        dr("AVAILABLE_TEST_ID") = 3
        dtAvailable.Rows.Add(dr)
        dr = Nothing

        'Row Three
        dr = dtAvailable.NewRow()
        dr("TEST_ID") = 2
        dr("AVAILABLE_TEST_ID") = 1
        dtAvailable.Rows.Add(dr)
        dr = Nothing

        'Row Four
        dr = dtAvailable.NewRow()
        dr("TEST_ID") = 2
        dr("AVAILABLE_TEST_ID") = 3
        dtAvailable.Rows.Add(dr)
        dr = Nothing

        'Row Five
        dr = dtAvailable.NewRow()
        dr("TEST_ID") = 3
        dr("AVAILABLE_TEST_ID") = 1
        dtAvailable.Rows.Add(dr)
        dr = Nothing

        'Row Six
        dr = dtAvailable.NewRow()
        dr("TEST_ID") = 3
        dr("AVAILABLE_TEST_ID") = 2
        dtAvailable.Rows.Add(dr)
        dr = Nothing

The above code would populate available table to look like this.

Code:

Test ID    Available Tests
-------    --------------
    1                2
    1                3
    2                1
    2                3
    3                1
    3                2

I have a function that add a selected records from the Available DataTable over to the Assigned DataTable and then removes the value from the Available DataTable. It also removes one hop circular references from the Available DataTable.

When the function is executed, it moves Pair 1,2 over to the Assigned DataTable and deletes both 1,2 and 2,1 from the Available DataTable.

Code:

Test ID    Assigned Tests
-------    --------------
    1                2

Test ID    Available Tests
-------    --------------
    1                3
    2                3
    3                1
    3                2


What I am trying to figure out is how to prevent multiple hop circular references. Where pairs 1,2 and 2,3 already exist in the Assigned DataTable. I want to prevent the user from selecting and populating pair 3,1...thus creating a circular reference where 1 needs 3, 2 needs 1, and 3 needs 2.

Code:

Test ID    Assigned Tests
-------    --------------
    1                2   
    2                3   

Test ID    Available Tests
-------    --------------

Here is what I got so far...I have tried several types of loops and circular functions, but have not figured it out yet...I am not sure how to proceed.

Code:

        For Each row As DataRow In dtAssigned.Rows
            For Each item As DataRow In dtAvailable.Select("TEST_ID = " & row("ASSIGNED_TEST_ID"))
                Dim deleteme As DataRow = dtAvailable.Select("TEST_ID = " & item("TEST_ID")).FirstOrDefault
                deleteme.Delete()
            Next
        Next

Thanks
jlimited

Viewing all articles
Browse latest Browse all 27034

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>