Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
314 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses

886 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