Saturday 31 October 2015

Create a Checkbox header column with check header in winforms DataGridView using VB.Net

In this post we are going to see how to create a check box header column in the datagridview , to make a functionality like select all option in one single click event or un-check all grid rows in single select.


1. Create a New Winform Project in VB language
2. Drag and Drop the DataGridView in the Form


Add a Model class name it as Employee.cs

Friend Class Employee

    Public Sub New(id As Integer, fname As String, lname As String)
        Me.Id = id
        FirstName = fname
        LastName = lname
    End Sub

    Private _firstname As String

    Public Property FirstName() As String
        Get
            Return _firstname
        End Get
        Set(ByVal value As String)
            _firstname = value
        End Set
    End Property

    Private _lastname As String

    Public Property LastName() As String
        Get
            Return _lastname
        End Get
        Set(ByVal value As String)
            _lastname = value
        End Set
    End Property

    Private _id As String

    Public Property Id() As String
        Get
            Return _id
        End Get
        Set(ByVal value As String)
            _id = value
        End Set
    End Property

End Class


Let we see how the DataGridView looks before the Checkheader cell assignment.





Now add a Class and name it as CheckBoxHeaderCell.vb

Imports System.Windows.Forms.VisualStyles

Public Class CheckBoxHeaderCell : Inherits DataGridViewColumnHeaderCell

    Public Event OnHeaderClick As CheckBoxHeaderClickHandler

    Dim CHECKBOX_SIZE As Size
    Dim ISCHECKED As Boolean
    Dim CHECKBOX_LOCATION As Point
    Dim CELL_BOUNDS As Point
    Dim CHECK_STATE As CheckBoxState = CheckBoxState.UncheckedNormal

    Public Sub New()
        CHECKBOX_LOCATION = New Point()
        CELL_BOUNDS = New Point()
        ISCHECKED = False
    End Sub

    Protected Overrides Sub Paint(graphics As Graphics, clipBounds As Rectangle,
                                  cellBounds As Rectangle, rowIndex As Integer,
                                  dataGridViewElementState As DataGridViewElementStates,
                                  value As Object, formattedValue As Object
                                  errorText As String,
                                  cellStyle As DataGridViewCellStyle,
                                  advancedBorderStyle As DataGridViewAdvancedBorderStyle,
                                  paintParts As DataGridViewPaintParts)

        MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, 
        value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)

        CHECKBOX_SIZE = CheckBoxRenderer.GetGlyphSize(graphics,CheckBoxState.UncheckedNormal)
        CHECKBOX_LOCATION.X = cellBounds.X + (cellBounds.Width/2 - CHECKBOX_SIZE.Width / 2)
        CHECKBOX_LOCATION.Y = cellBounds.Y + (cellBounds.Height/2 - CHECKBOX_SIZE.Height / 2)
        CELL_BOUNDS = cellBounds.Location

        If (ISCHECKED) Then
            CHECK_STATE = CheckBoxState.CheckedNormal
        Else
            CHECK_STATE = CheckBoxState.UncheckedNormal
        End If

        CheckBoxRenderer.DrawCheckBox(graphics, CHECKBOX_LOCATION, CHECK_STATE)

    End Sub

    Protected Overrides Sub OnMouseClick(e As DataGridViewCellMouseEventArgs)

        Dim TOUCHPOINT As Point
        TOUCHPOINT = New Point(e.X + CELL_BOUNDS.X, e.Y + CELL_BOUNDS.Y)

        If ((TOUCHPOINT.X > CHECKBOX_LOCATION.X And TOUCHPOINT.X < (CHECKBOX_LOCATION.X + 
             CHECKBOX_SIZE.Width)) And
            (TOUCHPOINT.Y > CHECKBOX_LOCATION.Y And TOUCHPOINT.Y < (CHECKBOX_LOCATION.Y + 
            CHECKBOX_SIZE.Height))) Then

            ISCHECKED = Not ISCHECKED
            If OnHeaderClickEvent IsNot Nothing Then
                Dim CHECK_ARGS = New CheckBoxHeaderCellEventArgs(ISCHECKED, CHECKBOX_SIZE, 
                                     CHECKBOX_LOCATION)

                RaiseEvent OnHeaderClick(Me, CHECK_ARGS)

            End If

        End If

        MyBase.OnMouseClick(e)

    End Sub

End Class


Add a EventHandler which to handle the click event

Public Delegate Sub 
CheckBoxHeaderClickHandler(sender As Object, e As CheckBoxHeaderCellEventArgs)


 Add a Event ARGS


Public Class CheckBoxHeaderCellEventArgs : Inherits EventArgs


    Private _checkBoxBounds As Point
    Public Property CheckBox_Bounds() As Point
        Get
            Return _checkBoxBounds
        End Get
        Set(ByVal value As Point)
            _checkBoxBounds = value
        End Set
    End Property
    Private _checkBoxSize As Size
    Public Property CheckBox_Size() As Size
        Get
            Return _checkBoxSize
        End Get
        Set(ByVal value As Size)
            _checkBoxSize = value
        End Set
    End Property
    Private _isChecked As Boolean
    Public Property IsChecked() As Boolean
        Get
            Return _isChecked
        End Get
        Set(ByVal value As Boolean)
            _isChecked = value
        End Set
    End Property

    Public Sub New(checked As Boolean, size As Size, location As Point)
        Me.IsChecked = checked
        Me.CheckBox_Bounds = location
        Me.CheckBox_Size = size
    End Sub

End Class

Add a Code in Form1

Public Class Form1

    Private WithEvents checkheader As CheckBoxHeaderCell = New CheckBoxHeaderCell()

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim chcol = New DataGridViewCheckBoxColumn()

        chcol.HeaderCell = checkheader
        DataGridView1.Columns.Add(chcol)
        DataGridView1.Rows.Add(4)

        Dim emplist = New List(Of Employee)
        emplist.Add(New Employee(1, "Rajesh""G"))
        emplist.Add(New Employee(2, "Suresh""G"))

        DataGridView1.DataSource = emplist


    End Sub

    Private Sub OnCheckHeaderClick(sender As Object, e As CheckBoxHeaderCellEventArgs
    Handles checkheader.OnHeaderClick
        DataGridView1.BeginEdit(True)
        For Each row As DataGridViewRow In DataGridView1.Rows
            row.Cells(0).Value = e.IsChecked
        Next
        DataGridView1.EndEdit()
    End Sub

End Class


Output :
                                 



From this you can see how to create a CheckBoxHeader cell in VB.Net