Solved

VB.net create Data Table out of 2 dimensional array

Posted on 2014-02-17
8
3,211 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks. I am using this for reporting purposes so have just strings will be fine
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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
Comment Utility
		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
Comment Utility
Thank you very much
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

10 Experts available now in Live!

Get 1:1 Help Now