Results 1 to 8 of 8

Thread: [RESOLVED] Join Datatables for local Reporting

Threaded View

  1. #6

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Resolved Re: Join Datatables for local Reporting

    So I have made a first Mock-Up of my effort to
    IT WORKS PERFECTLY! Join 4 hierarchically nested datatables in 1 single flattened datatable..

    Code is posted here 'as is' so that others my find it useful or at least as a starting point...
    If you have any ideas to refine this code (or part of it) I'm all ears
    VB.Net Code:
    1. 'I have 1 dataset and in that dataset 4 datatables with all needed columns created through designer
    2. 'every datatable name in original dataset has unique name (unique to all dataset and not just datatable)
    3. 'in every datatable columns are:
    4. '   first column is Int32 - autogeneratedID - unique to table
    5. '   second column is Int32 - ID - the same key as parent key (this field is binding conection to parent table: parent:child > one:many > autogeneratedID:ID
    6. '   other columns as you like
    7. 'Expected result is 1 new dataset with 1 new flattened datatable created totally through code which conteins all above 4 datatables. Result is showned in new form in a datagridview manner...
    8.  
    9. Private Sub FlattenAndShowDatatable_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripBtnPrint.Click
    10. 'original datatables        
    11.         Dim dsOrg As New DataSet
    12.         Dim dt1 As DataTable = ds1.dt1o.Copy
    13.         Dim dt2 As DataTable = ds1.dt2o.Copy
    14.         Dim dt3 As DataTable = ds1.dt3o.Copy
    15.         Dim dt4 As DataTable = ds1.dt4o.Copy
    16.         dsOrg.Tables.AddRange(New DataTable() {dt1, dt2, dt3, dt4})
    17. 'preparing for new dataset/datatable
    18.         Dim dsJoined As New DataSet
    19.         Dim dtJ As New DataTable
    20.         dsJoined.Tables.Add(dtJ)
    21.  
    22. 'last table in original dataset (when I reach the end row in this table it moves to one higher parent table (curentIndexTable-1)
    23.         Dim startTableIndx As Integer = dsOrg.Tables.Count - 1
    24.  
    25.         Do
    26.             Dim dr As DataRow = dtJ.NewRow
    27.             Dim keyparent As String = Nothing
    28.  
    29.             For iTbl As Integer = startTableIndx To 0 Step -1
    30.                 For iRow As Integer = 0 To dsOrg.Tables(iTbl).Rows.Count - 1
    31.  
    32.                     Dim alreadyExist As Boolean = False
    33.                     For i As Integer = 0 To dtJ.Rows.Count - 1
    34.                         If iTbl = startTableIndx AndAlso dtJ.Rows(i).Item(dsOrg.Tables(iTbl).Columns(0).ToString).ToString = dsOrg.Tables(iTbl).Rows(iRow).Item(0).ToString Then
    35.                             alreadyExist = True
    36.                             Exit For
    37.                         End If
    38.                     Next
    39.  
    40.                     If alreadyExist = False Then
    41.                         If dsOrg.Tables(iTbl).Rows(iRow).Item(0).ToString = keyparent Or keyparent = Nothing Then 'if its not correct parent, skip it
    42.                             For iCol As Integer = 0 To dsOrg.Tables(iTbl).Columns.Count - 1
    43. 'add column if column name not existing in table
    44.                                 If Not dsJoined.Tables(0).Columns.Contains(dsOrg.Tables(iTbl).Columns(iCol).ToString) Then
    45.                                     dsJoined.Tables(0).Columns.Add(dsOrg.Tables(iTbl).Columns(iCol).ToString)
    46.                                 End If
    47. 'HERE WE ACTUALLY POPULATING THE CELL IN NEW DATATABLE
    48.                                 dr(dsOrg.Tables(iTbl).Columns(iCol).ToString) = dsOrg.Tables(iTbl).Rows(iRow).Item(iCol)   'dr("ColumnName") = "aaa" ...add data to this column under that row
    49.                                 If iCol = 1 Then   'second Column contains ID of parent table
    50.                                     keyparent = dsOrg.Tables(iTbl).Rows(iRow).Item(iCol).ToString
    51.                                 End If
    52.                             Next iCol
    53.                             Exit For
    54.  
    55.                         End If
    56.                     End If
    57.  
    58.                     If iRow = dsOrg.Tables(startTableIndx).Rows.Count - 1 Then
    59.                         startTableIndx -= 1
    60.                     End If
    61.  
    62.                 Next iRow
    63.             Next iTbl
    64.  
    65. 'HERE WE ARE ACTUALLY ADDING A ROW POPULATED WITH CELLS TO NEW DATATABLE
    66.             dtJ.Rows.Add(dr)
    67.         Loop Until startTableIndx = -1
    68.  
    69. 'showing the results in new form with datagridview
    70.         Dim myForm As New Form
    71.         Dim dgv As New DataGridView
    72.         With dgv
    73.             .Name = "dgv"
    74.             .Location = New Point(15, 15)
    75.             .Dock = DockStyle.Fill
    76.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    77.         End With
    78.         myForm.Controls.Add(dgv)
    79.         dgv.DataSource = dtJ
    80.         'dgv.DataMember = "dtJ"
    81.         myForm.WindowState = FormWindowState.Maximized
    82.         myForm.Show()
    83.     End Sub
    Last edited by Zeljko; Aug 21st, 2011 at 01:46 AM.
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width