KeyLimeTie Blog

How to Add a WinForm DataGridView Header CheckBox

By Brian Pautsch – 10/10/2006 10:15:40 PM. Posted to Code Snippets.

A recent project required that we figure out how to add a "Select All" checkbox to the header column header of a DataGridViewCheckBoxColumn Windows Forms. After searching the web for possible solutions, it became clear that the actual control cound not be added dynamically. The best approach we could find was to paint a checkbox image into the header and respond to the DataGridView's "ColumnHeaderMouseClick" event. Here's the solution in detail:

Download source code



1. Create a Resource File and add in the two attached images. When I dragged them in, they were renamed to “_checked” and “_unchecked”.

2. Declare a “Select All” private variable.
1privatebool _selectAll = false;
3. Wherever you bind your DataGridView (I did in Page_Load for this example), add in the checkbox column (unless you already have it in your design view).

1private void Form1_Load(object sender, EventArgs e)
2{
3    try
4 { 5 //Bind XML dataset to DataGridView
6 DataSet ds = new DataSet(); 7 ds.ReadXml(_xmlFilePath); 8 dataGridView1.DataSource = ds.Tables[0]; 9 //Add the checkbox column
10 dataGridView1.Columns.Insert(0, 11 new DataGridViewCheckBoxColumn()); 12 } 13 catch (Exception ex) 14 { 15 MessageBox.Show("Exception: " + ex.ToString()); 16 } 17}
4. Create the CellPainting Event.

1privatevoid dataGridView1_CellPainting(object sender, 
2    DataGridViewCellPaintingEventArgs e)
3{
4    //Is this the checkbox column header?
5 if (e.RowIndex == -1 && e.ColumnIndex == 0) 6 { 7 try
8 { 9 //Erase the cell
10 using (Brush backColorBrush = 11 new SolidBrush(e.CellStyle.BackColor)) 12 { 13 e.Graphics.FillRectangle(backColorBrush, e.CellBounds); 14 } 15
16 //Draw 1 bottom line...
17 e.Graphics.DrawLine(Pens.DarkGray, e.CellBounds.Left, 18 e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1); 19 //Draw 2 top lines...
20 e.Graphics.DrawLine(Pens.DarkGray, e.CellBounds.Left, 21 e.CellBounds.Top, e.CellBounds.Right, e.CellBounds.Top); 22 e.Graphics.DrawLine(Pens.White, e.CellBounds.Left, 23 e.CellBounds.Top + 1, e.CellBounds.Right, e.CellBounds.Top + 1); 24 //Draw right line...
25 e.Graphics.DrawLine(Pens.DarkGray, e.CellBounds.Right - 1, 26 e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); 27 //Draw left line...
28 e.Graphics.DrawLine(Pens.White, e.CellBounds.Left, 29 e.CellBounds.Top, e.CellBounds.Left, e.CellBounds.Bottom); 30
31 //Get the image from the resource file
32 Image imgChecked = (Image)Resource1._checked; 33 Image imgUnchecked = (Image)Resource1._unchecked; 34
35 //Determine paint coordinates
36 int X = e.CellBounds.Left + 37 ((e.CellBounds.Width - imgChecked.Width) / 2) - 1; 38 int Y = e.CellBounds.Top + 39 ((e.CellBounds.Height - imgChecked.Height) / 2) - 1; 40
41 //Draw checkbox in header
42 if (_selectAll) 43 e.Graphics.DrawImage(imgChecked, X, Y); 44 else
45 e.Graphics.DrawImage(imgUnchecked, X, Y); 46
47 //Set event as handled
48 e.Handled = true; 49 } 50 catch
51 { 52 //Handle exception
53 } 54 } 55}
5. Handle the Select All click event.

1privatevoid dataGridView1_ColumnHeaderMouseClick(object sender, 
2    DataGridViewCellMouseEventArgs e)
3{
4    if (e.ColumnIndex == 0)
5    {
6        _selectAll = !_selectAll;
7        for (int i = 0; i < dataGridView1.Rows.Count; i++)
8        {
9            dataGridView1.Rows[i].Cells[0].Value = _selectAll;
10        }
11    }
12}

Comments

On 1/8/2010 di.arie said:
do u have for VB language.

On 1/8/2010 Bill said:
Use the EndEdit() method from the DataGridView object before changing values in ColumnHeaderMouseClick event.

On 11/20/2008 Duong Trong Nguyen said:
The source is missing the piece where a header checkbox should reflect the items checked from cells. Also, use the EndEdit() method from the DataGridView object to change the selected value.

On 6/22/2007 André Luis said:
Fantastic!

On 3/16/2007 todd said:
if the cell is selected, the displayed value is not changing

On 3/12/2007 Tiago Matos said:
Very nice!I'm using it.

On 1/3/2007 SH said:
Thanks just what I was looking for. Might want to post this up on Codeproject.

Leave a Comment

Name:
Email:
URL:
Comment:
Security Code:
Type Security Code:

Photos on Flickr

More Photos »

Search Blog


Get Email Updates

Like what you read here at KeyLimeTie? Sign up for our email list!

Subscribe