Wednesday, July 1, 2009

Custom Built -DataGridView with Select /Deselect all checkbox

The following code snippet is used to add CheckBox in Column Header of DataGridView.


/* When the Column Header check box is selected, all row check boxes will be selected */

/* When the Column Header check box is deselected, all row check boxes will be deselected */

/* When any row check box is deselected while the Column Header check box is selected, the column header check box will be deselected */

/*When the Column Header check box is selected while any of row check box is in edit mode, that checkbox will also get selected.


Dim bForceUnCheck As Boolean = False


Private Sub populateGridView()

'Declaration of variables

Dim strSql As String
Dim dt As New DataTable
Dim cmd As SqlCommand

Me.Cursor = Cursors.WaitCursor

strSql = "SELECT TOP " & nPageSize & " * FROM " & table

cmd = New SqlCommand(strSql, connection)

cmd.CommandTimeout = 999999

dataAdapter = New SqlDataAdapter(cmd)

Me.Cursor = Cursors.WaitCursor



Me.dbGridView.DataSource = dt


Me.Cursor = Cursors.Default

Catch ex As SqlException


Me.Cursor = Cursors.Default

Me.dbGridView.DataSource = Nothing

Exit Sub

End Try

' Customize DataGridView, add checkbox column

Dim checkboxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()

checkboxColumn.Name = "chkBox"

checkboxColumn.Width = 30

checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

'Add it is a First Column

DataGridView1.Columns.Insert(0, checkboxColumn)

' Add Checkbox Column Header

Dim rect As Rectangle = DataGridView1.GetCellDisplayRectangle(0, -1, True)

' set checkbox header to center of header cell. +1 pixel to position correctly.
rect.X = rect.Location.X + (rect.Width / 4)

Dim checkboxHeader As CheckBox = New CheckBox()

checkboxHeader.Name = "SELECTALL"

checkboxHeader.Size = New Size(18, 18)

checkboxHeader.Location = rect.Location

AddHandler checkboxHeader.CheckedChanged, AddressOf checkboxHeader_CheckedChanged


End Sub


'This Function handles the state change of Checkbox Column Header


Private Sub checkboxHeader_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)

If Not bForceUnCheck Then

DataGridView1.EndEdit()'To override the individually selected cells

For i As Integer = 0 To DataGridView1.RowCount - 1

DataGridView1(0, i).Value = CType(DataGridView1.Controls.Find("SELECTALL", True)(0), CheckBox).Checked


End If

bForceUnCheck = False

End Sub


'This function handles the CheckBox state changed event for each row of datagridview


Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

If CType(sender, DataGridView).Columns(e.ColumnIndex).Name = "chkBox" And e.RowIndex >= 0 Then

Dim dgCB As DataGridViewCheckBoxCell

dgCB = CType(DataGridView1("chkBox", e.RowIndex), DataGridViewCheckBoxCell)

'Uncheck the header checkbox when any of the check box in the row
'is deselected

If Not CBool(dgCB.EditedFormattedValue) Then

'Checkbox Unselected

If CType(DataGridView1.Controls.Find("SELECTALL", True)(0), CheckBox).Checked Then

bForceUnCheck = True

CType(DataGridView1.Controls.Find("SELECTALL", True)(0), CheckBox).Checked = False

End If

End If

End If

End Sub



  1. Exactly what I was looking for, thank you!

  2. Hi,

    Thank you for the code.

    I just wanted to add more flexibility to this code by letting the individual rows to be selectable as well. In this case the check is working only from the column header checkbox whereas i want even if i want to select random rows manually, i should be able to. Can you help plz?