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.

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...

33 comments

  1. 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

  2. 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.

  3. 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

  4. hi shyam, im sorry i cannot help you with this though, i have stopped c# development since i was done with a personal project that I was doing.

    what i can tell you though is that, if you have the documentation for c# api, you will know which property to use to detect which checkbox exactly was clicked. sorry i could not be of help as my laptop is running on mac os and i just deleted ms visual studio and its docs in my xp virtual machine

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

    Thanks …. it’s working good.. but the first row of check box is in select mode but not in check…

  6. hi,
    nice articles i have a question. what should be code in c# .net when gridview1 column5 data is the same as the gridview2 column2 data, now when the gridview1 column edit index change edit button is clicked, the edit index should check all the column2 values in the gridview2 and if matches the corresponding column value should be checked……………….please help me how to do this……….i am punished for not doing this……………..plz

  7. just use the same code and modify it. since the code loops through all checkboxes, you can change it when the checkbox index number matches the one you want to select/deselect

  8. Hi. I think theres a problem in adding the checkbox header inside the gridview. My datagridview has a vertical scroll bar and when I scroll farther to the right, the checkbox header doesn’t stick to its position, It always follows whenever I used the scroll bar. How can I resolve this?

  9. This code very usefull.. How to add “Delete” code based on this code?
    So, if I checked on the selected row, then the data will be deleted from tha datagridview also from database…

    Thanks in advance…

  10. Just add the following line:
    list.Columns[“checkboxHeader”].Frozen = true;

    before
    list.Controls.Add(checkboxHeader);

    It should stop the check box in the header to move when the user scroll horizontally

    1. @rashmi: the list variable is the table itself, though I did not include the code part of the initialization. it should be self explanatory if you already have background on c# development

    1. Private Sub frmITHealth_Load(sender As Object, e As EventArgs) Handles MyBase.Load

      For Each row As DataGridViewRow In grdservices.Rows

      row.Cells(0).Value = True

      Next
      End Sub

  11. Nice post, please how do i uncheck the header checkbox when the user uncheck all the checkboxes manually i want to be able to uncheck the header checkbox if all the checkboxes are being checked, and uncheck the header checkbox if all the checkboxes are being uncheck manually. Thanks

    1. @mfon: sorry, it’s been a long time since i touched c#. i dont have visual studio here with me now. you can google it though. that is something easy to do

Leave a Reply

Your email address will not be published. Required fields are marked *