VB.NET DataGridView - Fill from Dictionary

In this class we want to see how to populate a DataGridView from a Dictionary.

Our DataGridView will have two comlumns and we show the selected item from the datagridview in a messagebox.

Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Module Module1

    Private WithEvents myDataGridView As DataGridView
    Private executedFirstTime As Boolean = True

    Sub Main()
    End Sub

    Private Sub createForm()
        Dim myForm As Form = New Form With {.Text = "VB.NET WinForms : DataGridView - Fill From Dictionary - Camposha.info", .ClientSize = New Size(564, 520), .BackColor = Color.Coral}
    End Sub

    Private Sub setupDataGridView()
        myDataGridView = New DataGridView With {.Location = New Point(30, 70), .Size = New Size(500, 400), .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill, .SelectionMode = DataGridViewSelectionMode.FullRowSelect, .ColumnCount = 2}
        myDataGridView.Columns(0).Name = "NAME"
        myDataGridView.Columns(1).Name = "CATEGORY"
    End Sub

    Private Sub myDataGridView_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myDataGridView.SelectionChanged
        If executedFirstTime Then
            executedFirstTime = False
        End If

            Dim selectedIndex As Integer = myDataGridView.SelectedRows(0).Index
            If selectedIndex <> -1 Then
                If myDataGridView.SelectedRows(0).Cells(0).Value IsNot Nothing Then
                    Dim name As String = myDataGridView.SelectedRows(0).Cells(0).Value.ToString()
                End If
            End If
        Catch argumentOutOfRangeException As ArgumentOutOfRangeException
        End Try
    End Sub

    Private Sub populateData()
        Dim nebulae As Dictionary(Of String, String) = New Dictionary(Of String, String) From {{"Horse Head", "HII REGION"}, {"Black Widow", "REFLECTION NEBULAR"}, {"Ghost Head", "HII REGION"}, {"Witch Head", "PLANETARY NEBULAR"}, {"Cat's Eye", "DARK NEBULAR"}, {"Elephant's Trunk", "HII REGION"}, {"Helix", "SUPERNOVA REMNANTS"}, {"Rosette", "REFLECTION NEBULAR"}, {"Ant", "PLANETARY NEBULAR"}, {"Orion", "HII REGION"}, {"Eagle", "REFLECTION NEBULAR"}, {"Roesette", "PLANETARY NEBULAR"}, {"Bernad 68", "SUPERNOVA REMNANTS"}, {"Butterfly", "DARK NEBULAR"}, {"Snake", "DARK NEBULAR"}, {"Own", "REFLECTION NEBULAR"}, {"Ring", "SUPERNOVA REMNANTS"}, {"Pelican", "PLANETARY NEBULAR"}, {"Cone", "DARK NEBULAR"}, {"Flame", "REFLECTION NEBULAR"}, {"Bumerang", "SUPERNOVA REMNANTS"}}
        For Each nebular As KeyValuePair(Of String, String) In nebulae
            myDataGridView.Rows.Add(nebular.Key, nebular.Value)
    End Sub
End Module

Result VB.NET DataGridView Dictionary

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Previous Post Next Post