Add a select all checkbox at DataGridView header

05 Mar

These methods put a CheckBox at the first column header of the DataGridView to select/deselect all CheckBoxes in the first column of every DataGridViewRow. The CheckBox at the header also change its state according to the states of the CheckBoxes at each row. To apply it to a DataGridView:
1. Add a DataGridViewCheckBoxColumn as the first column in the DataGridView.
2. Copy the codes into the form.
3. Call AddSelectAllCheckBox with the DataGridView.

Private Sub MyForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub

'This member is necessary to distinqish if the header CheckBox is clicked
'in the CellValueChanged event.
Private _IsSelectAllChecked As Boolean

Private Sub AddSelectAllCheckBox(ByVal theDataGridView As DataGridView)
    Dim cbx As New CheckBox
    cbx.Name = "SelectAll"
    'The box size
    cbx.Size = New Size(14, 14)

    Dim rect As Rectangle
    rect = theDataGridView.GetCellDisplayRectangle(0, -1, True)
    'Put CheckBox in the middle-center of the column header.
    cbx.Location = New System.Drawing.Point(rect.Location.X + ((rect.Width - cbx.Width) / 2), rect.Location.Y + ((rect.Height - cbx.Height) / 2))
    cbx.BackColor = Color.White

    'Handle header CheckBox check/uncheck function
    AddHandler cbx.Click, AddressOf HeaderCheckBox_Click
    'When any CheckBox value in the DataGridViewRows changed,
    'check/uncheck the header CheckBox accordingly.
    AddHandler theDataGridView.CellValueChanged, AddressOf DataGridView_CellChecked
    'This event handler is necessary to commit new CheckBox cell value right after
    'user clicks the CheckBox.
    'Without it, CellValueChanged event occurs until the CheckBox cell lose focus
    'which means the header CheckBox won't display corresponding checked state instantly when user
    'clicks any one of the CheckBoxes.
    AddHandler theDataGridView.CurrentCellDirtyStateChanged, AddressOf DataGridView_CurrentCellDirtyStateChanged
End Sub

Private Sub HeaderCheckBox_Click(ByVal sender As Object, ByVal e As EventArgs)
    Me._IsSelectAllChecked = True

    Dim cbx As CheckBox
    cbx = DirectCast(sender, CheckBox)
    Dim theDataGridView As DataGridView = cbx.Parent

    For Each row As DataGridViewRow In theDataGridView.Rows
        row.Cells(0).Value = cbx.Checked


    Me._IsSelectAllChecked = False
End Sub

Private Sub DataGridView_CellChecked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
    Dim dataGridView As DataGridView = DirectCast(sender, DataGridView)
    If Not Me._IsSelectAllChecked Then
        If dataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = False Then
            'When any single CheckBox is unchecked, uncheck the header CheckBox.
            DirectCast(dataGridView.Controls.Item("SelectAll"), CheckBox).Checked = False
            'When any single CheckBox is checked, loop through all CheckBoxes to determine
            'if the header CheckBox needs to be unchecked.
            Dim isAllChecked As Boolean = True
            For Each row As DataGridViewRow In dataGridView.Rows
                If row.Cells(0).Value = False Then
                    isAllChecked = False
                    Exit For
                End If
            DirectCast(dataGridView.Controls.Item("SelectAll"), CheckBox).Checked = isAllChecked
        End If
    End If
End Sub

'The CurrentCellDirtyStateChanged event happens after user change the cell value,
'before the cell lose focus and CellValueChanged event.
Private Sub DataGridView_CurrentCellDirtyStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim dataGridView As DataGridView = DirectCast(sender, DataGridView)
    If TypeOf dataGridView.CurrentCell Is DataGridViewCheckBoxCell Then
        'When the value changed cell is DataGridViewCheckBoxCell, commit the change
        'to invoke the CellValueChanged event.
    End If
End Sub

Reference:Toggling the States of all CheckBoxes Inside a DataGridView Column


Posted by on March 5, 2012 in .NET, VB, Winform



2 responses to “Add a select all checkbox at DataGridView header

  1. tester

    October 24, 2013 at 10:11 PM

    thank you a lot for this code. It works. But try to use horizontal scrolling – looks fun.

  2. Anand

    December 16, 2013 at 3:05 AM

    Thank You So Much this coed it work


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: