Solved

VB.net create Data Table out of 2 dimensional array

Posted on 2014-02-17
8
3,298 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 62

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 62

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
 

Author Comment

by:murbro
ID: 39865094
Thanks. I am using this for reporting purposes so have just strings will be fine
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now