In this article I will explain with an example, how to add CheckBox Column to DataGridView control in Windows Forms (WinForms) application using C# and VB.Net.
This article will also explain how to get the row and cell (column) data for the rows for which the CheckBox is checked (selected) using C# and VB.Net.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Form Design
The Form1 consists of a DataGridView and a Button control which has been assigned a Click event handler.
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Populating the DataGridView from Database
Inside the Form Load event handler, the BindGrid function is called which fetches the records from Customers table using SqlDataAdapter and the data is loaded into a DataTable.
Finally, the DataTable is used to populate the DataGridView.
After the DataGridView is populated from Database, a DataGridViewCheckBoxColumn is created and added at the 0th Index in the DataGridView control.
C#
private const string ConnectionString = @"Data Source=.\SQL2008R2;Initial Catalog=Samples;Integrated Security = true";
public Form1()
{
InitializeComponent();
this.BindGrid();
}
private void BindGrid()
{
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
}
//Add a CheckBox Column to the DataGridView at the first position.
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "";
checkBoxColumn.Width = 30;
checkBoxColumn.Name = "checkBoxColumn";
dataGridView1.Columns.Insert(0, checkBoxColumn);
}
VB.Net
Private Const ConnectionString As String = "Data Source=.\SQL2008R2;Initial Catalog=Samples;Integrated Security = true"
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.BindGrid()
End Sub
Private Sub BindGrid()
Using con As New SqlConnection(ConnectionString)
Using cmd As New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using dt As New DataTable()
sda.Fill(dt)
dataGridView1.DataSource = dt
End Using
End Using
End Using
End Using
'Add a CheckBox Column to the DataGridView at the first position.
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = ""
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
dataGridView1.Columns.Insert(0, checkBoxColumn)
End Sub
Get selected CheckBox values from DataGridView on Button Click
When the Get Selected button is clicked the following event handler is executed, where a loop is executed over the DataGridView rows.
Inside the loop, first a check is performed whether the CheckBox value is TRUE or FALSE i.e. Checked or Unchecked. Then the value from the Name column is fetched.
Finally, the values of the Name column from the selected rows are displayed using MessageBox.
C#
private void btnGet_Click(object sender, EventArgs e)
{
string message = string.Empty;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
bool isSelected = Convert.ToBoolean(row.Cells["checkBoxColumn"].Value);
if (isSelected)
{
message += Environment.NewLine;
message += row.Cells["Name"].Value.ToString();
}
}
MessageBox.Show("Selected Values" + message);
}
VB.Net
Private Sub btnGet_Click(sender As System.Object, e As System.EventArgs) Handles btnGet.Click
Dim message As String = String.Empty
For Each row As DataGridViewRow In dataGridView1.Rows
Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("checkBoxColumn").Value)
If isSelected Then
message &= Environment.NewLine
message &= row.Cells("Name").Value.ToString()
End If
Next
MessageBox.Show("Selected Values" & message)
End Sub
Screenshots
DataGridView with CheckBox Column
Selected (Checked) row values displayed in MessageBox
Downloads