Public Class frmDataSetComponent
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lstOutput As System.Windows.Forms.ListBox
    Friend WithEvents dsEmployeeInfo As System.Data.DataSet
    Friend WithEvents DataTable1 As System.Data.DataTable
    Friend WithEvents DataColumn1 As System.Data.DataColumn
    Friend WithEvents DataColumn2 As System.Data.DataColumn
    Friend WithEvents DataColumn3 As System.Data.DataColumn
    Friend WithEvents DataTable2 As System.Data.DataTable
    Friend WithEvents DataColumn4 As System.Data.DataColumn
    Friend WithEvents DataColumn5 As System.Data.DataColumn
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lstOutput = New System.Windows.Forms.ListBox
        Me.dsEmployeeInfo = New System.Data.DataSet
        Me.DataTable1 = New System.Data.DataTable
        Me.DataColumn1 = New System.Data.DataColumn
        Me.DataColumn2 = New System.Data.DataColumn
        Me.DataColumn3 = New System.Data.DataColumn
        Me.DataTable2 = New System.Data.DataTable
        Me.DataColumn4 = New System.Data.DataColumn
        Me.DataColumn5 = New System.Data.DataColumn
        CType(Me.dsEmployeeInfo, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DataTable1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DataTable2, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'lstOutput
        '
        Me.lstOutput.Location = New System.Drawing.Point(72, 16)
        Me.lstOutput.Name = "lstOutput"
        Me.lstOutput.Size = New System.Drawing.Size(456, 485)
        Me.lstOutput.TabIndex = 0
        '
        'dsEmployeeInfo
        '
        Me.dsEmployeeInfo.DataSetName = "NewDataSet"
        Me.dsEmployeeInfo.Locale = New System.Globalization.CultureInfo("pl-PL")
        Me.dsEmployeeInfo.Relations.AddRange(New System.Data.DataRelation() {New System.Data.DataRelation("relDzialyPracownicy", "Dzialy", "Pracownicy", New String() {"ID"}, New String() {"IDDzialu"}, False)})
        Me.dsEmployeeInfo.Tables.AddRange(New System.Data.DataTable() {Me.DataTable1, Me.DataTable2})
        '
        'DataTable1
        '
        Me.DataTable1.Columns.AddRange(New System.Data.DataColumn() {Me.DataColumn1, Me.DataColumn2, Me.DataColumn3})
        Me.DataTable1.Constraints.AddRange(New System.Data.Constraint() {New System.Data.UniqueConstraint("Constraint1", New String() {"Imie", "Nazwisko"}, True), New System.Data.ForeignKeyConstraint("relDzialyPracownicy", "Dzialy", New String() {"ID"}, New String() {"IDDzialu"}, System.Data.AcceptRejectRule.None, System.Data.Rule.Cascade, System.Data.Rule.Cascade)})
        Me.DataTable1.PrimaryKey = New System.Data.DataColumn() {Me.DataColumn1, Me.DataColumn2}
        Me.DataTable1.TableName = "Pracownicy"
        '
        'DataColumn1
        '
        Me.DataColumn1.AllowDBNull = False
        Me.DataColumn1.ColumnName = "Imie"
        '
        'DataColumn2
        '
        Me.DataColumn2.AllowDBNull = False
        Me.DataColumn2.ColumnName = "Nazwisko"
        '
        'DataColumn3
        '
        Me.DataColumn3.ColumnName = "IDDzialu"
        Me.DataColumn3.DataType = GetType(System.Int32)
        '
        'DataTable2
        '
        Me.DataTable2.Columns.AddRange(New System.Data.DataColumn() {Me.DataColumn4, Me.DataColumn5})
        Me.DataTable2.Constraints.AddRange(New System.Data.Constraint() {New System.Data.UniqueConstraint("Constraint1", New String() {"ID"}, False), New System.Data.UniqueConstraint("Constraint2", New String() {"NazwaDzialu"}, True)})
        Me.DataTable2.MinimumCapacity = 5
        Me.DataTable2.PrimaryKey = New System.Data.DataColumn() {Me.DataColumn5}
        Me.DataTable2.TableName = "Dzialy"
        '
        'DataColumn4
        '
        Me.DataColumn4.AutoIncrement = True
        Me.DataColumn4.ColumnName = "ID"
        Me.DataColumn4.DataType = GetType(System.Int32)
        Me.DataColumn4.ReadOnly = True
        '
        'DataColumn5
        '
        Me.DataColumn5.AllowDBNull = False
        Me.DataColumn5.ColumnName = "NazwaDzialu"
        '
        'frmDataSetComponent
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(552, 525)
        Me.Controls.Add(Me.lstOutput)
        Me.Name = "frmDataSetComponent"
        Me.Text = "Komponent DataSet"
        CType(Me.dsEmployeeInfo, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DataTable1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DataTable2, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub frmDataSetComponent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddData()
        DisplayDataSet()
        DisplayChildRows(dsEmployeeInfo.Tables("Dzialy"))
    End Sub

    Private Sub AddData()
        Dim dtDepartments As DataTable = dsEmployeeInfo.Tables("Dzialy")
        Dim dtEmployees As DataTable = dsEmployeeInfo.Tables("Pracownicy")
        ' Dodawanie 4 rekordów do tabeli Dzialy
        Dim rowDept As DataRow
        rowDept = dtDepartments.NewRow
        rowDept("NazwaDzialu") = "Administracja"
        dtDepartments.Rows.Add(rowDept)
        rowDept = dtDepartments.NewRow
        rowDept("NazwaDzialu") = "Produkcja"
        dtDepartments.Rows.Add(rowDept)
        rowDept = dtDepartments.NewRow
        rowDept("NazwaDzialu") = "Sprzedaż"
        dtDepartments.Rows.Add(rowDept)
        rowDept = dtDepartments.NewRow
        rowDept("NazwaDzialu") = "Marketing"
        dtDepartments.Rows.Add(rowDept)

        ' Dodawanie 3 rekordów do tabeli Pracownicy
        Dim rowEmployee As DataRow
        rowEmployee = dtEmployees.NewRow
        rowEmployee("Imie") = "Janusz"
        rowEmployee("Nazwisko") = "Głowacki"
        rowEmployee("IDDzialu") = 2
        dtEmployees.Rows.Add(rowEmployee)

        rowEmployee = dtEmployees.NewRow
        rowEmployee("Imie") = "Jerzy"
        rowEmployee("Nazwisko") = "Makowiecki"
        rowEmployee("IDDzialu") = 3
        dtEmployees.Rows.Add(rowEmployee)

        rowEmployee = dtEmployees.NewRow
        rowEmployee("Imie") = "Szymon"
        rowEmployee("Nazwisko") = "Janowicz"
        rowEmployee("IDDzialu") = 3
        dtEmployees.Rows.Add(rowEmployee)

    End Sub

    Private Sub DisplayDataSet()
        Dim dr As DataRow
        Dim dc As DataColumn

        Me.lstOutput.Items.Add("WYŚWIETLANIE ZESTAWU DANYCH")
        Me.lstOutput.Items.Add("===========================")

        ' Wyświetlanie danych z tabeli Pracownicy
        For Each dr In dsEmployeeInfo.Tables("Pracownicy").Rows
            For Each dc In dsEmployeeInfo.Tables("Pracownicy").Columns
                Me.lstOutput.Items.Add(dc.ColumnName & " : " & dr(dc))
            Next
            Me.lstOutput.Items.Add("-----")
        Next
        Me.lstOutput.Items.Add("")
        ' Wyświetlanie danych z tabeli Dzialy
        ' Używamy indeksów zamiast instrukcji For Each
        Dim row As Integer
        Dim col As Integer

        For row = 0 To dsEmployeeInfo.Tables("Dzialy").Rows.Count - 1
            For col = 0 To dsEmployeeInfo.Tables("Dzialy").Columns.Count - 1
                Me.lstOutput.Items.Add( _
                 dsEmployeeInfo.Tables("Dzialy").Columns(col).ColumnName _
                        & " : " & _
                        dsEmployeeInfo.Tables("Dzialy").Rows(row)(col))
            Next col
            Me.lstOutput.Items.Add("-----")
        Next row
    End Sub

    Private Sub DisplayChildRows(ByVal dt As DataTable)
        Dim rel As DataRelation
        Dim relatedRows() As DataRow
        Dim row As DataRow
        Dim col As DataColumn
        Dim i As Integer
        Dim rowData As String

        Me.lstOutput.Items.Add("")
        Me.lstOutput.Items.Add("WIERSZE PODRZĘDNE")

        For Each row In dt.Rows
            For Each rel In dt.ChildRelations
                Me.lstOutput.Items.Add( _
                  dt.TableName & ": " & _
                  rel.ParentColumns(0).ColumnName & _
                  "= " & row(rel.ParentColumns(0).ToString))

                relatedRows = row.GetChildRows(rel)
                ' Wyświetlanie wartości wierszy
                For i = 0 To relatedRows.GetUpperBound(0)
                    rowData = "**** " & _
                                rel.ChildTable.TableName & ":"
                    For Each col In rel.ChildTable.Columns
                        rowData = rowData & " " & _
                                relatedRows(i)(col.ToString)
                    Next col
                    Me.lstOutput.Items.Add(rowData)
                Next i
            Next rel
        Next row
    End Sub

End Class
