The DataGridView Control


I. Summary

The DataGridView_Example Project shows how to populate a DataGridView using a DataGridView Class. In addition, The DataGridView_Example demonstrates how to use a ComboBox Class to populate the ComboBoxes of a DataGridView Control. This example can also be used for databases by populating the Properties of the Objects with the Field Values of a Recordset.

II. Screenshots

The DataGridView Control The DataGridView Control

III. Sample Code

A. The cSoftwareCompany Class

Public Class cSoftwareCompany
Private lSoftwareCompanyID As Integer
Private lSoftwareCompanyName as String
Private lSoftwareCompanyCity As String
Private lSoftwareCompanyState As String
Private lSoftwareCompanyCountryID As Integer

Public Property pSoftwareCompanyID() As Integer
Get
pSoftwareCompanyID = lSoftwareCompanyID
End Get
Set(ByVal iSoftwareCompanyID As Integer)
lSoftwareCompanyID = iSoftwareCompanyID
End Set
End Property

Public Property pSoftwareCompanyName() As String
Get
pSoftwareCompanyName = lSoftwareCompanyName
End Get
Set(ByVal sSoftwareCompanyName As String)
lSoftwareCompanyName = sSoftwareCompanyName
End Set
End Property

Public Property pSoftwareCompanyCity() As String
Get
pSoftwareCompanyCity = lSoftwareCompanyCity
End Get
Set(ByVal sSoftwareCompanyCity As String)
lSoftwareCompanyCity = sSoftwareCompanyCity
End Set
End Property

Public Property pSoftwareCompanyState() As String
Get
pSoftwareCompanyState = lSoftwareCompanyState
End Get
Set(ByVal sSoftwareCompanyState As String)
lSoftwareCompanyState = sSoftwareCompanyState
End Set
End Property

Public Property pSoftwareCompanyCountryID() As Integer
Get
pSoftwareCompanyCountryID = lSoftwareCompanyCountryID
End Get
Set(ByVal iSoftwareCompanyCountryID As Integer)
lSoftwareCompanyCountryID = iSoftwareCompanyCountryID
End Set
End Property

End Class

B. The cCountry Class

Public Class cCountry
Private lCountryID As Integer
Private lCountryName As String

Public Property pCountryID() As Integer
Get
pCountryID = lCountryID
End Get
Set(ByVal iCountryID As Integer)
lCountryID = iCountryID
End Set
End Property

Public Property pCountryName() As String
Get
pCountryName = lCountryName
End Get
Set(ByVal sCountryName As String)
lCountryName = sCountryName
End Set
End Property

End Class

C. The frmDataGridView Form Load Event

Private Sub frmDataGridView_Load(sender As Object, e As EventArgs) Handles Me.Load
On Error Goto Errorhandler

'Initialize Variables
'-----------------------------------------

Dim oSoftwareCompanyList As List(Of cSoftwareCompany)
Dim oSoftwareCompany(3) As cSoftwareCompany
Dim oCountryComboBoxList As List(Of cCountry)
Dim oCountryComboBox(1) as cCountry
Dim oDataGridTextBoxColumn As DataGridViewTextBoxColumn
Dim oDataGridComboBoxColumn As DataGridViewComboBoxColumn

oSoftwareCompanyList = New List(Of cSoftwareCompany)
oCountryComboBoxList = New List(Of cCountry)

'Set the Properties of the DataGridView
'-----------------------------------------

Me.dgSoftwareCompany.AllowUserToAddRows = False
Me.dgSoftwareCompany.AutoGenerateColumns = False
Me.dgSoftwareCompany.MultiSelect = False

'Initialize Software Company Object and Add To Software Company List
'-----------------------------------------

oSoftwareCompany(0) = New cSoftwareCompany
With oSoftwareCompany(0)
.pSoftwareCompanyID = 1
.pSoftwareCompanyName = "Microsoft"
.pSoftwareCompanyCity = "Redmond"
.pSoftwareCompanyState = "Washington"
.pSoftwareCompanyCountryID = 1
End With
oSoftwareCompanyList.Add(oSoftwareCompany(0))

oSoftwareCompany(1) = New cSoftwareCompany
With oSoftwareCompany(1)
.pSoftwareCompanyID = 2
.pSoftwareCompanyName = "Apple"
.pSoftwareCompanyCity = "Cupertino"
.pSoftwareCompanyState = "California"
.pSoftwareCompanyCountryID = 1
End With
oSoftwareCompanyList.Add(oSoftwareCompany(1))

oSoftwareCompany(2) = New cSoftwareCompany
With oSoftwareCompany(2)
.pSoftwareCompanyID = 3
.pSoftwareCompanyName = "Oracle"
.pSoftwareCompanyCity = "Redwood City"
.pSoftwareCompanyState = "California"
.pSoftwareCompanyCountryID = 1
End With
oSoftwareCompanyList.Add(oSoftwareCompany(2))

oSoftwareCompany(3) = New cSoftwareCompany
With oSoftwareCompany(3)
.pSoftwareCompanyID = 3
.pSoftwareCompanyName = "SAP"
.pSoftwareCompanyCity = "Walldorf"
.pSoftwareCompanyState = "Baden-Württemberg"
.pSoftwareCompanyCountryID = 2
End With
oSoftwareCompanyList.Add(oSoftwareCompany(3))

'Populate Data Grid With Software Company List
'-----------------------------------------

Me.dgSoftwareCompany.DataSource = oSoftwareCompanyList

'Software Company ID
oDataGridTextBoxColumn = New DataGridViewTextBoxColumn
With oDataGridTextBoxColumn
.HeaderText = "Software Company ID"
.DataPropertyName = "pSoftwareCompanyID"
.Width = "75"
.Visible = True
Me.dgSoftwareCompany.Columns.Add(oDataGridTextBoxColumn)
End With
oDataGridTextBoxColumn = Nothing

'Software Company Name
oDataGridTextBoxColumn = New DataGridViewTextBoxColumn
With oDataGridTextBoxColumn
.HeaderText = "Software Company Name"
.DataPropertyName = "pSoftwareCompanyName"
.Width = "150"
.Visible = True
Me.dgSoftwareCompany.Columns.Add(oDataGridTextBoxColumn)
End With
oDataGridTextBoxColumn = Nothing

'Software Company City
oDataGridTextBoxColumn = New DataGridViewTextBoxColumn
With oDataGridTextBoxColumn
.HeaderText = "Software Company City"
.DataPropertyName = "pSoftwareCompanyCity"
.Width = "150"
.Visible = True
Me.dgSoftwareCompany.Columns.Add(oDataGridTextBoxColumn)
End With
oDataGridTextBoxColumn = Nothing

'Software Company State
oDataGridTextBoxColumn = New DataGridViewTextBoxColumn
With oDataGridTextBoxColumn
.HeaderText = "Software Company State"
.DataPropertyName = "pSoftwareCompanyState"
.Width = "150"
.Visible = True
Me.dgSoftwareCompany.Columns.Add(oDataGridTextBoxColumn)
End With
oDataGridTextBoxColumn = Nothing

'Software Company Country
oDataGridComboBoxColumn = New DataGridViewComboBoxColumn
With oDataGridComboBoxColumn
.HeaderText = "Software Company Country"
.DataPropertyName = "pSoftwareCompanyCountryID"
.Width = "150"
.Visible = True

'Add ComboBoxValue to Country DataGridComboBox
oCountryComboBox(0) = New cCountry
With oCountryComboBox(0)
.pCountryID = 1
.pCountryName = "United States"
End With
oCountryComboBoxList.Add(oCountryComboBox(0))

oCountryComboBox(1) = New cCountry
With oCountryComboBox(1)
.pCountryID = 2
.pCountryName = "Germany"
End With
oCountryComboBoxList.Add(oCountryComboBox(1))

oDataGridComboBoxColumn.DataSource = oCountryComboBoxList
oDataGridComboBoxColumn.DisplayMember = "pCountryName"
oDataGridComboBoxColumn.ValueMember = "pCountryID"
End With
Me.dgSoftwareCompany.Columns.Add(oDataGridComboBoxColumn)
oDataGridComboBoxColumn = Nothing

'Clean Up Variables
'-----------------------------------------

oSoftwareCompany = Nothing
oSoftwareCompanyList = Nothing

Exit Sub
Errorhandler:
Msgbox(Err.Number & "-" & Err.Source & ": " & Err.Description)
End Sub