DataGridView with CheckBox at Header
-
Wednesday, May 16, 2012 5:20 AM
Hello,
How to add a Check Box in the header of a check box column? I need to add a check box in the header.
All Replies
-
Wednesday, May 16, 2012 8:33 AM
You cannot add a checkbox in a Row Header,
Please describe next time more exact that you want, now I see what you want.
It is simply not possible for that you need 3rd party grids or use WPF (I assume the WPF datagrid will also not fit like, you have to design it completely yourself).
However, you can make of course the rowheader invisible and use the first column
Success
Cor
- Edited by Cor LigthertMVP Wednesday, May 16, 2012 8:35 AM
- Edited by Cor LigthertMVP Wednesday, May 16, 2012 8:35 AM
-
Wednesday, May 16, 2012 1:59 PM
Not supported by the DataGridView, but you can add one. I used the code from the below link:
http://www.brianpautsch.com/Blog/2006/10/10/DataGridView/
Private _selectAll As Boolean = False 'Fill your DataGridView then add the CheckBox column: DataGridView1.Columns.Insert(0, New DataGridViewCheckBoxColumn()) Private Sub DataGridView1_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting 'Is this the checkbox column header? If e.RowIndex = -1 AndAlso e.ColumnIndex = 0 Then Try 'Erase the cell Using backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor) e.Graphics.FillRectangle(backColorBrush, e.CellBounds) End Using 'Draw 1 bottom line... e.Graphics.DrawLine(Pens.DarkGray, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1) 'Draw 2 top lines... e.Graphics.DrawLine(Pens.DarkGray, e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Right, e.CellBounds.Top) e.Graphics.DrawLine(Pens.White, e.CellBounds.Left, e.CellBounds.Top + 1, e.CellBounds.Right, e.CellBounds.Top + 1) 'Draw right line... e.Graphics.DrawLine(Pens.DarkGray, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom) 'Draw left line... e.Graphics.DrawLine(Pens.White, e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Left, e.CellBounds.Bottom) 'Get the image from the resource file Dim imgChecked As Image = CType(Code_Examples.My.Resources.Resource1.checked, Image) Dim imgUnchecked As Image = CType(Code_Examples.My.Resources.Resource1.unchecked, Image) 'Determine paint coordinates Dim X As Integer = e.CellBounds.Left + ((e.CellBounds.Width - imgChecked.Width) / 2) - 1 Dim Y As Integer = e.CellBounds.Top + ((e.CellBounds.Height - imgChecked.Height) / 2) - 1 'Draw checkbox in header If _selectAll Then e.Graphics.DrawImage(imgChecked, X, Y) Else e.Graphics.DrawImage(imgUnchecked, X, Y) End If 'Set event as handled e.Handled = True Catch 'Handle exception End Try End If End Sub Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick If e.ColumnIndex = 0 Then _selectAll = Not _selectAll For i As Integer = 0 To DataGridView1.Rows.Count - 1 DataGridView1.Rows(i).Cells(0).Value = _selectAll Next i End If End Sub
Paul ~~~~ Microsoft MVP (Visual Basic)
- Marked As Answer by Saravana Kumar Rajen Wednesday, May 16, 2012 10:40 PM
- Unmarked As Answer by Saravana Kumar Rajen Tuesday, May 22, 2012 12:36 AM
-
Monday, May 21, 2012 1:00 AM
One good solution can be found here:
CheckBox in Header Cell of DataGridView Control
Pradeep, Microsoft MVP (Visual Basic)
http://pradeep1210.wordpress.com- Marked As Answer by Saravana Kumar Rajen Tuesday, May 22, 2012 12:36 AM

