Adding CheckBox to DataGridView in VB.NET

The DataGridView control offers various column types that allow for displaying and modifying data in different ways. These column types include TextBox, CheckBox, Image, Button, ComboBox, and Link, each with their corresponding cell types.

In the following VB.NET program, we demonstrate how to add a CheckBox column to a DataGridView control and set the value of the checkbox in the third row to true. This enables users to interact with the checkboxes within the DataGridView.

Full Source VB.NET
Imports System.Data.SqlClient Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click DataGridView1.ColumnCount = 3 DataGridView1.Columns(0).Name = "Product ID" DataGridView1.Columns(1).Name = "Product Name" DataGridView1.Columns(2).Name = "Product_Price" Dim row As String() = New String() {"1", "Product 1", "1000"} DataGridView1.Rows.Add(row) row = New String() {"2", "Product 2", "2000"} DataGridView1.Rows.Add(row) row = New String() {"3", "Product 3", "3000"} DataGridView1.Rows.Add(row) row = New String() {"4", "Product 4", "4000"} DataGridView1.Rows.Add(row) Dim chk As New DataGridViewCheckBoxColumn() DataGridView1.Columns.Add(chk) chk.HeaderText = "Check Data" chk.Name = "chk" DataGridView1.Rows(2).Cells(3).Value = True End Sub End Class

By handling the CellClick event, you can respond immediately when users click a checkbox cell. However, note that this event occurs before the cell value is updated. Therefore, you can access the previous value of the checkbox cell and perform any necessary actions based on that value.

Conclusion

With the flexibility provided by the DataGridView control and its column types, you can create interactive and user-friendly interfaces that allow users to modify data using various cell types, such as checkboxes.