VB.net create Data Table out of 2 dimensional array

Posted on 2014-02-17
Medium Priority
Last Modified: 2014-02-17

I gather "cell" values into the following two dimensional array. How do I convert this two dimensional array into a data table that can be bound to a GridView/DataGridView

Dim arrCells(99999999, 9999999) As String

An example of the full data table would be:
Company      2007      2008      2009
ABC Infotech      6522817.82      844951.63      1056676.54
TPS Software      7972686.64      874160.19      950163.45
XYZ InfoSystem      266929.36      999524.14      1723025.95
Question by:Murray Brown
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
  • 4
  • 2
  • 2
LVL 63

Expert Comment

by:Fernando Soto
ID: 39864978
Your array only has two dimensions which can represent two columns with many rows but your example data table has four columns, how do you resolve this?

Author Comment

by:Murray Brown
ID: 39864995

But two dimensions can be used to store unlimited rows and columns
e.g. all the cells of an Excel spread sheet where the first dimension holds
the row number and the second the column number. Make sense?
LVL 63

Expert Comment

by:Fernando Soto
ID: 39865073
You do understand that to create an array of, Dim arrCells(99999999, 9999999) As String, Will be inefficient.

That said you will need to manually create a DataTable structure giving each column a name and data type. If the first row of the array(0,X) holds the columns name and because it is an array of string the data type will String and the column name is the value in that element of the array. Once that is done it is all a matter of iterating through the rows of the array and filling in the values for each cell. If all columns or rows are not used you will need to check to if it has a value by checking to see if the value of the element is equal to Nothing.
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!


Author Comment

by:Murray Brown
ID: 39865094
Thanks. I am using this for reporting purposes so have just strings will be fine
LVL 40
ID: 39865246
By "gather", I understand that you retrieve that data and build the array yourself? Why not build the DataTable directly instead of building the array?

And you will never be able to make this work, because the number of columns in a DataGridView is limites to 65535.

And as for the rows, I just made a little test, and adding such a number of rows with take a very long time. On a good computer with a lot of memory, it takes about 10 seconds to get only 115000 empty rows. If there is data, specially strings, and when it will start using the swap file...

Author Comment

by:Murray Brown
ID: 39865656

Thanks. I am only using this approach to pivot data so number of rows and columns won't be that big. What VB.net code would I use to directly build a data table using my small example above?

LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 2000 total points
ID: 39865890
		Dim table As New DataTable
		Dim arrCells(3, 3) As String

		'Fill your array and then

		table.Columns.Add(New DataColumn(arrCells(0, 0)))
		table.Columns.Add(New DataColumn(arrCells(0, 1)))
		table.Columns.Add(New DataColumn(arrCells(0, 2)))
		table.Columns.Add(New DataColumn(arrCells(0, 3)))

		For x As Integer = 1 To arrCells.GetUpperBound(0)
			Dim row As DataRow = table.NewRow
			For y As Integer = 0 To arrCells.GetUpperBound(1)
				row.Item(y) = arrCells(x, y)

Open in new window


Author Closing Comment

by:Murray Brown
ID: 39866598
Thank you very much

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

770 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