Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding datatable columns

Posted on 2015-02-24
5
Medium Priority
?
117 Views
Last Modified: 2015-02-26
Hi,

I after some help with logic.  I start off with a datatable that looks like this (in reality it may have more of less columns)

Col1      Col2      Col3
               
               
I then have another datatable which could have any number of rows (for this example I’ll assume it has 3 rows)
I need some mechanism of looping through the columns in the first datatable and for each row in the 2nd table insert corresponding columns.  The tricky part is the order; the first datatable must look like this when finished:

Col1      NewCol1      NewCol2      NewCol3      Col2      NewCol1      NewCol2      NewCol3      Col3      NewCol1      NewCol2      NewCol3
                                                                              
                                                                     
I had some success using the set ordinal method buts it not right.  Hoping a fresh pair’s eyes on the problem will help me out.

Thanks.
0
Comment
Question by:andyw27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40
ID: 40629008
What is your problem with SetOrdinal?

An alternative, would be to create a new DataTable and add the rows in the order that you need to add them, something like the following (not tested except for the fact that it compiles):

		Dim newTable As New DataTable
		Dim y As Integer 'Pointer into the second table

		For x As Integer = 0 To firstTable.Columns.Count - 1
			newTable.Columns.Add(firstTable.Columns(x))
			y += 1
			While <Your condition to add columns>  'Or any mechanism you use to know when to stop inserting from the second table
				newTable.Columns.Add(secondTable.Columns(y))
				y += 1
			End While
		Next

Open in new window

0
 

Author Comment

by:andyw27
ID: 40629069
I can't seem to get the order right, either I get exception out of range error or the order is wrong.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 40629116
Out of range is usually easy to pinpoint. If you set an Ordinal of 20 and you table has only 15 columns, there is something wrong in the logic.

If you are not getting the order right, then there is something wrong in your logic.

How do you determine which fields should fit between Col1 and Col2? How do you move through the columns in each of the 2 tables. Do you use a loop? A common error when looping through collections that start at 0 as the case with the Columns collection to go from 0 To Columns.Count. This works with collections that are indexed from 1, but from 0, you need to go 0 To Columns.Count-1. This is the kind of stuff that causes Out of range errors.

I won't be there for a few hours, but maybe somebody can see the problem by looking at your code.
0
 

Author Comment

by:andyw27
ID: 40629140
OK cheers, I determine the columns to add by looping through a second table, for each row I add a column, I then jump by DT.rows.count which in theory puts it ready to insert the next columns.
I agree its a logic error, I can't access the code at the moment but I post when I get a chance.  If anybody fancies having a crack at it in the interim that would be appreciated.
0
 

Author Comment

by:andyw27
ID: 40633370
Cracked it.  Just created a int list with the insert positions already defined, then looped through the datatable containging the columns to be added.

You can have the points seeing as you're the only person who actaully responded - many thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

722 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