473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DataGridView checkbox column header cell (.Net 4.0).

88 64KB
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
0 2171

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

Similar topics

1
by: Morten Snedker | last post by:
Using datagridview: ..Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight still leaves a lot of space to the right border of the column header - any good reason...
2
by: Kiran_Juikar | last post by:
How to add the header checkbox in the datagridview checkbox column? Is there any property available to add a header checkboxor one need to write the custom logic for this?
0
by: SyB | last post by:
Hi, Does anyone know where I can find info/example on adding a checkbox into a header cell of a DataGridViewCheckBoxColumn. Cheers, Sy
3
by: David Veeneman | last post by:
I'm trying to set images in a bound DataGridView image column. My enum has three possible values, and I have an image that I want to show for each value: EnumValue.OK --GreenLight.gif...
3
by: Bill Nguyen | last post by:
I can't figure out how to identify/display the column header name in Mouseclick event. This is a checkbox column type and I would like all cells in this column to toggle checked/unchecked when the...
0
by: Simonsson | last post by:
I'm trying (using C#) to put an image (Icon or Bitmap maybe) in the column header cell of a DataGridView. I have no problems putting images in other cells. I do that like this: myDataRow =...
1
by: Ben | last post by:
Hi, I'm designing a c# page and trying to add a checkbox column to a GridView, I've added a Template Row (as described at: http://aspnet.4guysfromrolla.com/articles/052406-1.aspx) and in the...
2
by: Steve | last post by:
Hi All I am using VB.net 2005 windows forms I use the Datagridview.cellformatting event to change the colours of individual cells as required and all works fine How can I do the same for...
4
by: Benysh | last post by:
Hi everyone. I'm having a really annoying problem I can't solve and couldn't find an answer to anywhere, let's see if you got what it takes. I have a dataGridView on a C# Windows Form. Unbound....
3
by: sheridan | last post by:
Hi everyone. I'm hoping somone can help me, because I've been stuck on this all evening and haven't found the answer online yet. I have a DataGridView control and have managed to change all of...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.