By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,819 Members | 1,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,819 IT Pros & Developers. It's quick & easy.

DataGridView checkbox column header cell (.Net 4.0).

P: 79
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:



Expand|Select|Wrap|Line Numbers
  1. ''' <summary>
  2. ''' The custom class for checkbox header cell.
  3. ''' </summary>
  4. Imports System.Drawing
  5.  
  6. Public Delegate Sub CheckBoxClickedHandler(ByVal state As Boolean)
  7.  
  8. Public Class DataGridViewCheckBoxHeaderCellEventArgs
  9.     Inherits EventArgs
  10.     Private _bChecked As Boolean
  11.  
  12.     Public Sub New(ByVal bChecked As Boolean)
  13.         _bChecked = bChecked
  14.     End Sub
  15.  
  16.     Public ReadOnly Property Checked As Boolean
  17.         Get
  18.             Return _bChecked
  19.         End Get
  20.     End Property
  21. End Class
  22.  
  23. Public Class DataGridViewCheckBoxHeaderCell
  24.     Inherits DataGridViewColumnHeaderCell
  25.     Private checkBoxLocation As Point
  26.     Private checkBoxSize As Size
  27.     Private _checked As Boolean = False
  28.     Private _cellLocation As New Point
  29.     Private _cbState As Windows.Forms.VisualStyles.CheckBoxState = VisualStyles.CheckBoxState.UncheckedNormal
  30.     Public Event OnCheckBoxClicked As CheckBoxClickedHandler
  31.  
  32.     Public Sub New()
  33.     End Sub
  34.  
  35.     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)
  36.         MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
  37.  
  38.         Dim p As New Point
  39.         Dim s As Size = CheckBoxRenderer.GetGlyphSize(graphics, Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal)
  40.         p.X = cellBounds.Location.X + (cellBounds.Width / 2) - (s.Width / 2)
  41.         p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2)
  42.         _cellLocation = cellBounds.Location
  43.         checkBoxLocation = p
  44.         checkBoxSize = s
  45.  
  46.         If _checked Then
  47.             _cbState = Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal
  48.         Else
  49.             _cbState = Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal
  50.             CheckBoxRenderer.DrawCheckBox(graphics, checkBoxLocation, _cbState)
  51.         End If
  52.     End Sub
  53.  
  54.     Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
  55.         Dim p As New Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y)
  56.         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
  57.             _checked = Not _checked
  58.             RaiseEvent OnCheckBoxClicked(_checked)
  59.             Me.DataGridView.InvalidateCell(Me)
  60.         End If
  61.  
  62.         MyBase.OnMouseClick(e)
  63.     End Sub
  64. End Class
  65.  
  66. ''' <summary>
  67. ''' Actual class where the custom class is used.
  68. ''' dgvContacts is the name of the datagridview.
  69. ''' colCheck is the name of the column containing the checkbox header cell.
  70. ''' </summary>
  71. Public Class ContactList
  72. 'Other declarations...
  73. ......................
  74. ......................
  75. Private WithEvents _cbHeader As DataGridViewCheckBoxHeaderCell
  76.  
  77. Sub New()
  78.         ' This call is required by the designer.
  79.         InitializeComponent()
  80.  
  81.         ' Add any initialization after the InitializeComponent() call.
  82.     _cbHeader = New DataGridViewCheckBoxHeaderCell
  83.         colCheck.HeaderCell = _cbHeader
  84.         colCheck.HeaderText = ""
  85.         colCheck.Visible = True
  86. End Sub
  87.  
  88. 'Other definitions...
  89. .....................
  90. .....................
  91.  
  92. Private Sub _cbHeader_OnCheckBoxClicked(ByVal state As Boolean) Handles _cbHeader.OnCheckBoxClicked
  93.         For i As Integer = 0 To dgvContacts.RowCount - 1
  94.             dgvContacts("colCheck", i).Value = state
  95.         Next
  96.  
  97.         dgvContacts.EndEdit()
  98. End Sub
  99. End Class
  100.  
  101.  
Sep 13 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.