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()
        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

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:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post