Solved

VB.net create Data Table out of 2 dimensional array

Posted on 2014-02-17
8
3,488 Views
Last Modified: 2014-02-17
Hi

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
0
Comment
Question by:murbro
  • 4
  • 2
  • 2
8 Comments
 
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?
0
 

Author Comment

by:murbro
ID: 39864995
Hi

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?
0
 
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.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:murbro
ID: 39865094
Thanks. I am using this for reporting purposes so have just strings will be fine
0
 
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...
0
 

Author Comment

by:murbro
ID: 39865656
Hi

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?

Thanks
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 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)
			Next
			table.Rows.Add(row)
		Next

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 39866598
Thank you very much
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 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