VB.NET DataGridView - Fill from Dictionary

May 3, 2018 Oclemy VB.NET DataGridView, VB.NET Dictionary 1 minute, 34 seconds

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()
        setupDataGridView()
        createForm()
    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}
        myForm.Controls.Add(myDataGridView)
        Application.EnableVisualStyles()
        Application.Run(myForm)
    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"
        populateData()
    End Sub

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

        Try
            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()
                    MessageBox.Show(name)
                End If
            End If
        Catch argumentOutOfRangeException As ArgumentOutOfRangeException
            MessageBox.Show(argumentOutOfRangeException.Message)
        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)
        Next
    End Sub
End Module

Result VB.NET DataGridView Dictionary

Comments