Advertisement

[x]
Attachment Details

Merging Data Tables with vb.Net

[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8
Zone: .NET
Tags: ,
I am merging two data tables.  First I create both data tables.  I then set up a primary key for each table in code.  Then I run a loop that sets the structure.  Then I run the merge function.  

After I return the new data table I am getting all of the records back.  I only want to get back records where the primary keys were equal.  

Here is my code.  I have a data table called dt and one called dtcore.  Then im outputting the merged data table called dts.


                        ' DataColumn array to set primary key.
                        Dim keyCol(1) As DataColumn

                        ' Set primary key column.
                        keyCol(0) = dt.Columns(0)
                        dt.PrimaryKey = keyCol

                        ' Add RowChanged event handler for the table.
                        AddHandler dt.RowChanged, AddressOf Row_Changed

                        ' Set primary key column.
                        keyCol(0) = dtCore.Columns(0)
                        dtCore.PrimaryKey = keyCol

                        ' Add RowChanged event handler for the table.
                        AddHandler dtCore.RowChanged, AddressOf Row_Changed

                        ds.AcceptChanges()

                        Dim i As Integer

                        With dt
                            For i = 0 To .Columns.Count - 1
                                If Not dt.Columns.Contains(.Columns(i).ColumnName) Then
                                    dt.Columns.Add(New DataColumn(.Columns(i).ColumnName, .Columns(i).DataType))
                                End If

                            Next
                        End With

                        With dtCore
                            For i = 0 To .Columns.Count - 1
                                If Not dtCore.Columns.Contains(.Columns(i).ColumnName) Then
                                    dtCore.Columns.Add(New DataColumn(.Columns(i).ColumnName, .Columns(i).DataType))
                                End If
                            Next
                        End With

                        ds.Tables.Add(dtCore)
                        ds.Tables.Add(dt)
                        Console.WriteLine("Merging")
                        dtCore.Merge(dt, False, MissingSchemaAction.Ignore)


                        Dim dts As New System.Data.DataTable("merged")
                        dts = ds.Tables(0)


                        ' Dim appExcel As New Excel.Application

                        'For displaying the column name in the the excel file.
                        For iCol = 0 To dts.Columns.Count - 1
                            appExcel.Cells(1, iCol + 1).Value = dts.Columns(iCol).ColumnName.ToString
                            appExcel.Columns.EntireColumn.AutoFit()
                        Next

                        'For displaying the column value row-by-row in the the excel file.
                        For iRow2 = 0 To dt.Rows.Count - 1
                            For iColVal2 = 0 To dts.Columns.Count - 1
                                appExcel.Cells(iRow2 + 2, iColVal2 + 1).Value = Trim(dts.Rows(iRow2).ItemArray(iColVal2).ToString)
                            Next
Related Solutions
Related Solutions
 
Loading Advertisement...
 
Author Comment by robbhill:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 

Rank: Wizard

Expert Comment by graye:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Author Comment by robbhill:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Author Comment by robbhill:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 

Rank: Wizard

Expert Comment by graye:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Author Comment by robbhill:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 

Rank: Wizard

Accepted Solution by graye:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Author Comment by robbhill:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Author Comment by robbhill:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 

Rank: Wizard

Expert Comment by graye:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Author Comment by robbhill:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
Loading Advertisement...
20080924-EE-VQP-40