DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0).
Go Back  Xtreme Visual Basic Talk > > > DataGridView checkbox column header cell (.Net 4.0).


Reply
 
Thread Tools Display Modes
  #1  
Old 09-13-2011, 09:04 AM
priyamtheone priyamtheone is offline
Regular
 
Join Date: May 2008
Posts: 72
Question DataGridView checkbox column header cell (.Net 4.0).


Hi,
I'm trying to render a checkbox in a datagridview column header so that when the checkbox is checked/unchecked all the cells in that column should be
checked/unchecked accordingly. The datagridview is bound to a database table but the column containing the checkbox header cell is unbound.
The problem is, whenever i click the header checkbox, all the cells in that column are checked but the header checkbox itself gets invisible.
If I click the HEADER (though the checkbox is invisible, still I can click the header) again, all the cells in the column below are unchecked and the header
checkbox becomes visible and unchecked. Everything is happening as expected except the visibility of the header checkbox. Why is it so?
Please suggest how to get rid of it. Regards.
My code follows below:

Code:
''' <summary>
''' The custom class for checkbox header cell.
''' </summary>
Imports System.Drawing

Public Delegate Sub CheckBoxClickedHandler(ByVal state As Boolean)

Public Class DataGridViewCheckBoxHeaderCellEventArgs
    Inherits EventArgs
    Private _bChecked As Boolean

    Public Sub New(ByVal bChecked As Boolean)
        _bChecked = bChecked
    End Sub

    Public ReadOnly Property Checked As Boolean
        Get
            Return _bChecked
        End Get
    End Property
End Class

Public Class DataGridViewCheckBoxHeaderCell
    Inherits DataGridViewColumnHeaderCell
    Private checkBoxLocation As Point
    Private checkBoxSize As Size
    Private _checked As Boolean = False
    Private _cellLocation As New Point
    Private _cbState As Windows.Forms.VisualStyles.CheckBoxState = VisualStyles.CheckBoxState.UncheckedNormal
    Public Event OnCheckBoxClicked As CheckBoxClickedHandler

    Public Sub New()
    End Sub

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

        Dim p As New Point
        Dim s As Size = CheckBoxRenderer.GetGlyphSize(graphics, Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal)
        p.X = cellBounds.Location.X + (cellBounds.Width / 2) - (s.Width / 2)
        p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2)
        _cellLocation = cellBounds.Location
        checkBoxLocation = p
        checkBoxSize = s

        If _checked Then
            _cbState = Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal
        Else
            _cbState = Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal
            CheckBoxRenderer.DrawCheckBox(graphics, checkBoxLocation, _cbState)
        End If
    End Sub

    Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
        Dim p As New Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y)
        If p.X >= checkBoxLocation.X And p.X <= checkBoxLocation.X + checkBoxSize.Width And p.Y >= checkBoxLocation.Y And p.Y <= checkBoxLocation.Y + checkBoxSize.Height Then
            _checked = Not _checked
            RaiseEvent OnCheckBoxClicked(_checked)
            Me.DataGridView.InvalidateCell(Me)
        End If

        MyBase.OnMouseClick(e)
    End Sub
End Class

''' <summary>
''' Actual class where the custom class is used.
''' dgvContacts is the name of the datagridview.
''' colCheck is the name of the column containing the checkbox header cell.
''' </summary>
Public Class ContactList
'Other declarations...
......................
......................
Private WithEvents _cbHeader As DataGridViewCheckBoxHeaderCell

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

        ' Add any initialization after the InitializeComponent() call.
	_cbHeader = New DataGridViewCheckBoxHeaderCell
        colCheck.HeaderCell = _cbHeader
        colCheck.HeaderText = ""
        colCheck.Visible = True
End Sub

'Other definitions...
.....................
.....................

Private Sub _cbHeader_OnCheckBoxClicked(ByVal state As Boolean) Handles _cbHeader.OnCheckBoxClicked
        For i As Integer = 0 To dgvContacts.RowCount - 1
            dgvContacts("colCheck", i).Value = state
        Next

        dgvContacts.EndEdit()
End Sub
End Class
Reply With Quote
  #2  
Old 09-16-2011, 12:02 AM
NFITC1 NFITC1 is offline
Centurion
 
Join Date: Apr 2006
Posts: 102
Default

Quote:
Originally Posted by priyamtheone View Post
Code:
        If _checked Then
            _cbState = Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal
        Else
            _cbState = Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal
            CheckBoxRenderer.DrawCheckBox(graphics, checkBoxLocation, _cbState)
        End If
You're problem, if you haven't fixed it by now, is with these lines. The guy I work under is always saying that solutions are right in front of you, but sometimes it just takes an extra set of eyes to see it.
I believe what you meant was this:
Code:
        If _checked Then
            _cbState = Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal
        Else
            _cbState = Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal
        End If
        CheckBoxRenderer.DrawCheckBox(graphics, checkBoxLocation, _cbState)
Reply With Quote
  #3  
Old 09-16-2011, 09:27 AM
priyamtheone priyamtheone is offline
Regular
 
Join Date: May 2008
Posts: 72
Default

Ah! My Bad. I shouldn't have overlooked it. Thanks to triangulate it.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0). DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0).
 
DataGridView checkbox column header cell (.Net 4.0).
DataGridView checkbox column header cell (.Net 4.0).
 
-->