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.

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.

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.

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

Related Posts Plugin for WordPress, Blogger...