C#: Add Select All, Deselect All Checkbox In Column Header In DataGridView Control


Adding controls to your DataGridView like a checkbox as a column is possible with the use of the DataGridViewCheckboxColumn class. This post assumes that you are using the DataGridView control in a Windows.Form application and that your gridview object can already populate data into it. The purpose of this post is to add a column containing checkboxes including a header checkbox, used to select and deselect all in one click.

The name of our DataGridView control is list. To add a checkbox column into the gridview control, use the following code below inside your className_Load() method.

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// customize dataviewgrid, add checkbox column
DataGridViewCheckBoxColumn checkboxColumn = new DataGridViewCheckBoxColumn();
checkboxColumn.Width = 30;
checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
list.Columns.Insert(0, checkboxColumn);
 
// add checkbox header
Rectangle rect = list.GetCellDisplayRectangle(0, -1, true);
// set checkbox header to center of header cell. +1 pixel to position correctly.
rect.X = rect.Location.X + (rect.Width / 4);
 
CheckBox checkboxHeader = new CheckBox();
checkboxHeader.Name = "checkboxHeader";
checkboxHeader.Size = new Size(18, 18);
checkboxHeader.Location = rect.Location;
checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);
 
list.Controls.Add(checkboxHeader);

The method that does the selecting and deselecting of all checkboxes within the checkbox column is made possible with the CheckedChanged event. We add an event handler for this to make it work.

?View Code CSHARP
1
2
3
4
5
6
private void checkboxHeader_CheckedChanged(object sender, EventArgs e) {
    for (int i = 0; i < list.RowCount; i++) {
	list[0, i].Value = ((CheckBox) list.Controls.Find("checkboxHeader", true)[0]).Checked;
    }
    list.EndEdit();
}

That’s all there is to it. If you may wonder how to make the checkbox header align to the center, the code that makes that happen is the part where there is a Rectangle object used.

?View Code CSHARP
1
2
3
4
// add checkbox header
Rectangle rect = list.GetCellDisplayRectangle(0, -1, true);
// set checkbox header to center of header cell. +1 pixel to position correctly.
rect.X = rect.Location.X + (rect.Width4);

I divided the cell area’s width by 4 instead of 2 because I had to take into account the checkbox header’s width value. By the way, you also need to set the DataGridView control’s AutoSizeColumnsMode property to Fill so that all any empty spaces within the DataGridView control will be occupied

Donations appreciated. Every little $ helps. Or click Google +1

Related Posts with Thumbnails

20 Responses to “C#: Add Select All, Deselect All Checkbox In Column Header In DataGridView Control”

Pages: [1] 2 3 4 » Show All

  1. 1
    Prasy Says:

    Can i get the code to find the rows which are selected?

    Thanks
    Prasy

  2. 2
    tech Says:

    hey prasy, you can use this.

    for (int i=0; i if (Convert.ToBoolean(dgv.Rows[i].Cells[0].Value)) {
    // do anything here if checkbox is clicked
    }
    }

    where dgv is the name of the datagridview. and 0 is the first column where all your checkbox contains. this was the code i used in my project which was for the delete button.

  3. 3
    Shyam Says:

    Hi,

    Can you please tell how we can check/unceck the checkbox of particular column. And after selecting the checkbox as checked how can we retrieve tha row data and display it on a pannel.

    Thanks,
    Shyam

  4. 4
    tech Says:

    shyam, hi

    sorry i have not used c# for quite some time now. you can check the properties of the column in the help section and look which property gives what you need

    dgv.Rows[i].Cells[0]

    where dgv is the datagridview object, rows[i] is the row and cells[0] is the 1st column of the row since its index is zero.

  5. 5
    Shyam Says:

    Hi,
    Thnks for your quick reply..

    I am having a datagrid in which I am able to read data from my text file. And I have also created 0th coloumn as my checkbox column. I am able to select all the check boxes when I click the header checkbox. But I too want to select single checkbox at a time, which I am not able to do.. After clicking the particular check box I want to display each column of the particular row into the textboxes.

    Can you please tell me some logic for this.

    Thanks,
    Shyam

Pages: [1] 2 3 4 » Show All

Leave a Reply

Spam protection by WP Captcha-Free