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
            Return _firstname
        End Get
        Set(ByVal value As String)
            _firstname = value
        End Set
    End Property

    Private _lastname As String

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

    Private _id As String

    Public Property Id() As String
            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 ISCHECKED As Boolean
    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
            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)

             CHECKBOX_SIZE.Width)) And
            CHECKBOX_SIZE.Height))) Then

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

                RaiseEvent OnHeaderClick(Me, CHECK_ARGS)

            End If

        End If


    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
            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
            Return _checkBoxSize
        End Get
        Set(ByVal value As Size)
            _checkBoxSize = value
        End Set
    End Property
    Private _isChecked As Boolean
    Public Property IsChecked() As Boolean
            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.

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

        chcol.HeaderCell = checkheader

        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
        For Each row As DataGridViewRow In DataGridView1.Rows
            row.Cells(0).Value = e.IsChecked
    End Sub

End Class

Output :

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