Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.net create Data Table out of 2 dimensional array

Posted on 2014-02-17
8
Medium Priority
?
4,241 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:Murray Brown
  • 4
  • 2
  • 2
8 Comments
 
LVL 64

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:Murray Brown
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 64

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
Independent Software Vendors: 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
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:Murray Brown
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 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)
			Next
			table.Rows.Add(row)
		Next

Open in new window

0
 

Author Closing Comment

by:Murray Brown
ID: 39866598
Thank you very much
0

Featured Post

Independent Software Vendors: 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

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 …
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…
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

810 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