Solved

Finding out if a column in a DataTable has a relationship to another DataTable, and if so, to which column in that DataTable

Posted on 2007-11-30
2
302 Views
Last Modified: 2010-08-05
When looping through the columns of a DataTable in a DataSet in VB 2005, how can I find out if a column is linked (i.e. has a relatioship) to another DataTable in that DataSet, and if it is linked, to which column and DataTable?

Thanks
0
Comment
Question by:SETP
2 Comments
 
LVL 27

Accepted Solution

by:
VBRocks earned 500 total points
ID: 20382911
It's very easy, you can just loop through the Relations collection of the DataSet.  

Here's an example:

        'Create 2 tables, add them to a DataSet and create a relationship between both:
        Dim tableA As New DataTable()
        tableA.Columns.Add("tableA_ID")
        tableA.Columns("tableA_ID").DataType = GetType(Int16)
        tableA.Columns.Add("Item")

        Dim tableB As New DataTable()
        tableB.Columns.Add("tableB_ID")
        tableB.Columns("tableB_ID").DataType = GetType(Int16)
        tableB.Columns.Add("tableA_ID")
        tableB.Columns("tableA_ID").DataType = GetType(Int16)
        tableB.Columns.Add("Item")


        'Create a DataSet and add both tables
        Dim ds As New DataSet()
        ds.Tables.Add(tableA)
        ds.Tables.Add(tableB)


        'Create a relationship between both tables:
        ds.Relations.Add("tableA_tableB", _
            New DataColumn() {tableA.Columns("tableA_ID")}, _
            New DataColumn() {tableB.Columns("tableA_ID")}, _
           False)



        'Now to find out what types of relationships exist in the DataSet,
        '    between which tables, and what columns are referenced:
        For Each rel As DataRelation In ds.Relations
            Debug.WriteLine("Parent table:  " & rel.ParentTable.TableName)

            For Each col As DataColumn In rel.ParentColumns
                Debug.WriteLine(vbTab & "Parent column:  " & col.ColumnName)
            Next

            Debug.WriteLine("Child table:  " & rel.ChildTable.TableName)

            For Each col As DataColumn In rel.ChildColumns
                Debug.WriteLine(vbTab & "Child column:  " & col.ColumnName)
            Next
        Next

        Stop

0
 

Author Closing Comment

by:SETP
ID: 31411925
Thanks VBRocks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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