VB.NET DataGridView - Readonly DataGridViewCheckBoxColumn

VB.NET Readonly DataGridViewCheckBoxColumn Tutorial and Example

This is a VB.NET tutorial. We see how to populate a datagridviewview with dictionary containing strings and boolean values. The thing is that our boolean values should not be editable at runtime. Instead when the user clicks any cell in the datagridview, he should get the value shown in messagebox and not be able to edit it.

Let's start.

Video Tutorial(ProgrammingWizards TV Channel)

Well we have a video tutorial as an alternative to this. If you prefer tutorials like this one then it would be good you subscribe to our YouTube channel. Basically we have a TV for programming where do daily tutorials.


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

Module Program
    Private WithEvents myDataGridView As DataGridView
    Private ReadOnly checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()

    Private Sub setupDataGridView()
        myDataGridView = New DataGridView With {
            .Location = New Point(30, 70),
            .Size = New Size(500, 400),
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
            .SelectionMode = DataGridViewSelectionMode.CellSelect,
            .[ReadOnly] = True,
            .ColumnCount = 1
        myDataGridView.Columns(0).Name = "NAME"
        checkBoxColumn.Name = "ACTIVE STAR FORMATION?"
    End Sub

    Private Sub myDataGridView_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles myDataGridView.CellClick
        If e.RowIndex = -1 Then Return
        Dim selectedNebular As String = myDataGridView.Rows(e.RowIndex).Cells(0).Value.ToString()
        Dim nebularState As String = myDataGridView.Rows(e.RowIndex).Cells(1).Value.ToString()

        Select Case e.ColumnIndex
            Case 0
                MessageBox.Show(selectedNebular, "Nebular", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Case 1
                MessageBox.Show(nebularState, "Nebular", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Select
    End Sub

    Private Sub populateData()
        Dim nebulae As Dictionary(Of String, Boolean) = New Dictionary(Of String, Boolean) From {
            {"Horse Head", True},
            {"Black Widow", False},
            {"Ghost Head", True},
            {"Witch Head", False},
            {"Cat's Eye", False},
            {"Elephant's Trunk", False},
            {"Helix", False},
            {"Rosette", False},
            {"Ant", True},
            {"Orion", True},
            {"Eagle", True},
            {"Bernad 68", False},
            {"Butterfly", True},
            {"Snake", False},
            {"Own", True},
            {"Ring", True},
            {"Pelican", False},
            {"Cone", True},
            {"Flame", True},
            {"Bumerang", False}

        For Each nebular As KeyValuePair(Of String, Boolean) In nebulae
            myDataGridView.Rows.Add(nebular.Key, nebular.Value)
    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

    Sub Main()
    End Sub
End Module

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