Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Dim bs as BindingSource = New BindingSource
Dim bl as ArrayBL = New ArrayBL(aData) ' the custom BindingList class
bs.DataSource = bl
dgvArray.DataSource = bs ' bind the grid
' A factory base class for creating objects representing a row in a
' two-dimensional array.
Public Class ArrayRowFactory
Private _rowType As Type
Public Sub New(ByVal rowType As Type)
If rowType.IsSubclassOf(GetType(GenericArrayRow)) Then
_rowType = rowType
Else
Throw New ArgumentException
End If
End Sub
Public Property RowType() As Type
Get
Return _rowType
End Get
Set(ByVal value As Type)
If value.IsSubclassOf(GetType(GenericArrayRow)) Then
_rowType = value
Else
Throw New ArgumentException
End If
End Set
End Property
' factory method for creating array row objects
Public Function CreateRowObject(ByVal arr As System.Array, ByVal row As Integer) As GenericArrayRow
Dim args(1) As Object
args(0) = arr
args(1) = row
' use reflection to intantiate a row object of the correct type
Return Activator.CreateInstance(_rowType, args)
End Function
End Class
' Base class for array row classes.
Public Class GenericArrayRow
Protected _arr As System.Array
Protected _row As Integer
Protected Sub New()
End Sub
Public Sub New(ByVal arr As System.Array, ByVal row As Integer)
' save a reference to the parent array of this row
_arr = arr
' save the index value of this row in the parent array
_row = row
End Sub
' no methods declared; classes that implement this interface
' must create a property for each column in the array
End Class
' A representation of a single row of data within the 3 column,
' two-dimensional array being used as a data source
Public Class ArrayRow
Inherits GenericArrayRow
#Region "--- Constructors ---"
Protected Sub New()
End Sub
Public Sub New(ByVal arr As System.Array, ByVal row As Integer)
MyBase.New(arr, row)
End Sub
#End Region
' the following properties could be given more descriptive names to
' represent their actual contents, such as "FirstName," "LastName,"
' etc.
#Region "--- Column Properties ---"
Public Property Col0() As String
Get
Return _arr(_row, 0)
End Get
Set(ByVal value As String)
_arr(_row, 0) = value
End Set
End Property
Public Property Col1() As String
Get
Return _arr(_row, 1)
End Get
Set(ByVal value As String)
_arr(_row, 1) = value
End Set
End Property
Public Property Col2() As String
Get
Return _arr(_row, 2)
End Get
Set(ByVal value As String)
_arr(_row, 2) = value
End Set
End Property
#End Region
End Class
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Public Class ArrayDGForm
Dim aData As System.Array
Dim bs As BindingSource
Dim bl As New BindingList(Of ArrayRow)
Private Sub ArrayDG_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' fill array with sample data
aData = Array.CreateInstance(GetType(String), 5, 6)
For i As Integer = 0 To 4
For j As Integer = 0 To 5
aData(i, j) = "(i=" & i & ", j=" & j & ")"
Next
Next
' bind array to grid
bs = New BindingSource
Dim rowFact As New ArrayRowFactory(GetType(ArrayRow))
For i As Integer = aData.GetLowerBound(0) To aData.GetUpperBound(0)
bl.Add(rowFact.CreateRowObject(aData, i))
Next
bs.DataSource = bl
dgvArray.DataSource = bs
End Sub
Private Sub btnDump_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDump.Click
TextBox1.Text = String.Empty
For i As Integer = aData.GetLowerBound(0) To aData.GetUpperBound(0)
For j As Integer = aData.GetLowerBound(1) To aData.GetUpperBound(1)
TextBox1.Text = TextBox1.Text & aData(i, j) & Constants.vbTab
Next
TextBox1.Text = TextBox1.Text & Constants.vbCrLf
Next
End Sub
End Class