Earlier on this page, we used parallel arrays with a common index. That method is bulky and awkward at best. In their infinite
wisdom, the developers of Visual Basic anticipated this circumstance and allowed for multidimensional arrays. For this example, we will use a two-dimensional
array for rows and columns, but the concept extends to 32 dimensions.
To demonstrate the concept of two-dimensional arrays we will create a 4 x 4
Two Dimensional Array with contents as indicated in diagram below:
Figure Array-28: Visual Basic Select Data Grid View
Once you place it on the form change the size in the Visual Basic Properties Window to about 450 x 125 as indicated
in the diagram below:
Figure Array-29: Visual Basic Set Data Grid View Size
There are several properties that can allow you to precisely set the size of the Data Grid Data elements, but for now we will
just approximate the size to expedite completing the two-dimensional array example. We will use the default name of DataGridView1, since it is the only control
on our form. When you have finished setting the size property, your design form should look like the diagram below:
Figure Array-30: Visual Basic Data Grid Design View
Help Support this site - Click this ad
Double click on an area of the form not covered by the Visual Basic DataGridView control to bring up the
Visual Basic Source Code Window. This will create the Form1_Load subroutine that will execute our code without having to program buttons to display
the data. We will model our Visual Basic DataGridView after an Excel spreadsheet. Enter the code displayed below into the Form1_Load subroutine:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Const NumberOfColumns As Integer = 4
Const NumberOfRows As Integer = 4
Dim ColumnNumber As Integer
Dim RowNumber As Integer
Dim A As Integer = Asc("A")
Dim Array2D(NumberOfRows, NumberOfColumns) As Integer
DataGridView1.ColumnCount = NumberOfColumns
DataGridView1.RowCount = NumberOfRows
'Make the headers
For RowNumber = 0 To NumberOfRows - 1
DataGridView1.Rows.Item(RowNumber).HeaderCell.Value = _
(RowNumber + 1).ToString
Next
For ColumnNumber = 0 To NumberOfColumns - 1
DataGridView1.Columns.Item(ColumnNumber).HeaderCell.Value = _
Chr(A + ColumnNumber).ToString
DataGridView1.Columns.Item(ColumnNumber).HeaderCell.Style.Alignment = _
DataGridViewContentAlignment.MiddleCenter
Next
'Fill the Array
For RowNumber = 0 To NumberOfRows - 1
For ColumnNumber = 0 To NumberOfColumns - 1
Array2D(RowNumber, ColumnNumber) = _
RowNumber * NumberOfColumns + ColumnNumber
Next
Next
'Fill the Data Grid
For RowNumber = 0 To NumberOfRows - 1
For ColumnNumber = 0 To NumberOfColumns - 1
DataGridView1.Item(ColumnNumber, RowNumber).Value = _
Array2D(RowNumber, ColumnNumber)
DataGridView1.Item(ColumnNumber, RowNumber).Style.Alignment = _
DataGridViewContentAlignment.MiddleCenter
Next
Next
End Sub
End Class
Figure Array-31: Visual Basic Form_Load Source Code for 2-Dimensional Array
Help Support this site - Click this ad
Since there are several new concepts introduced in this code, we should probably comment on those now, so we understand
how the program functions:
Const NumberOfColumns As Integer = 4: Sets the current maximum size of columns to 4.
Const declares a value that cannot be changed after the initial declaration.
Const NumberOfRows As Integer = 4: Sets the current maximum size of rows to 4.
Dim ColumnNumber As Integer: Index to step through the Columns in our array.
Dim RowNumber As Integer: Index to step through the Rows in our array.
Dim A As Integer = Asc("A"): The variable A has the Ascii equivalent of the letter A
This will allow us to count: A, B, C, D instead of 1, 2, 3, 4
Dim Array2D(NumberOfRows, NumberOfColumns) As Integer: Declaring our 4 x 4 two-dimensional Array.
DataGridView1.ColumnCount= NumberOfColumns: Sets the number of columns(width) of our data display.
DataGridView1.RowCount=NumberOfRows: Sets the number of Rows(length) of our data display.
Help Support this site - Click this ad
The next 2 For Loops place names in the Header Cells.
For RowNumber=0 to NumberOfRows-1: Index through every Vertical Element
.Item(ColumnNumber, RowNumber): Same data cell we wrote the data to.
.Style: Defines the text style attribute used in the data cell.
.Alignnment=: The text attribute that will be changed is the text alignment in the data cell.
DataGridViewContentAlignment: How Visual Basic refers to the cell's text alignment attribute.
.MiddleCenter: Align the text in the middle center of the cell.
Because I think middle center alignment looks better.
Looks like it's time to take this puppy out for a test spin, and see what we get. Press the
Green Triangle to run the program and see what it looks like...
When I run it, I get the output as shown in the diagram below:
Figure Array-32: Visual Basic DataGridView at RunTime
Not too shabby. You can move the cursor around and change the values on the screen, although without the proper event
routines, the actual data isn't updated, the screen just changes.
In the next section we talk about the
Length and Sort methods of the array class. To demonstrate these characteristics we
work with the
DataGridView controls a little more to gain some more familiarity with them.
In order to continue this guide and work with Visual Basic Array Methods,
press the
Button Below: