How to add "select all " Checkbox in combobox column header


  I have Datagridview with a combobox column, i need to add a checkbox to the header of this column. if user selects(Checks)  this header checbox then all the checkboxes of this column should be selected. And if user Deselects(not Checked) then all the checkboxes of this column should be deselected.

  Basically i need to implement  select all/Deselect all feature........I am new to datagridview.
Please provide examples and sample code.

 Thanks in advance.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfred A.Commented:
Inside your ASP.NET Source do this


<script language="javascript" type="text/javascript">
        function SelectAllCheckboxes(spanChk){
            var oItem = spanChk.children;
            var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];

            if(elm[i].type=="checkbox" && elm[i].id!

//Check the template field  Action below.  Thiis is link to the javascript above

<asp:GridView ID="gvTest" runat="server" AllowPaging="True" AllowSorting="True"
        AutoGenerateColumns="False" CellPadding="4" DataKeyNames="Code" DataSourceID="sdsData"
        ForeColor="#333333" GridLines="None" Width="856px">
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#E3EAEB" HorizontalAlign="Left" />

            <asp:TemplateField HeaderText="Action">
                    <asp:CheckBox ID="chkAction" runat="server" />
                    <input id="chkAll" runat="server" onclick="javascript:SelectAllCheckboxes(this);"
                        type="checkbox" value="Action" />&nbsp;
                    <asp:CheckBox ID="chkAction" runat="server" />

I hope this helps.
Alfred A.Commented:
Oh, I didn't notice the DataGridView (winforms).  Sorry the code above is for ASP.NET.  However, you could still use the Javascript implementation in there for your DataGridView.

Alfred A.Commented:
Try this in a Windows Form Test Project
public partial class Form1 : Form
        DataGridViewCheckBoxColumn c1 = new DataGridViewCheckBoxColumn();
        private CheckBox ckBox;

        public Form1()

        private void Form1_Load(object sender, EventArgs e)

            c1 = new DataGridViewCheckBoxColumn();
            c1.Name = "Select";
            c1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

            CheckBox ckBox = new CheckBox();

            //Get the column header cell bounds
            Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);
            ckBox.Size = new Size(18, 18);
            //Change the location of the CheckBox to make it stay on the header
            ckBox.Location = rect.Location;

            ckBox.CheckedChanged += ckBox_CheckedChanged;

            //Add the CheckBox into the DataGridView

        private void ckBox_CheckedChanged(object sender, EventArgs e)
            for (int j = 0; j <= dataGridView1.RowCount - 1; j++)
               dataGridView1[0, j].Value = ((CheckBox)sender).Checked;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Maverick_CoolAuthor Commented:
Hi Alfred1,

    Thanks a lot for ur reply. Your suggestion helped me to do my task.

Alfred A.Commented:
Glad to help.  :-)

Goodluck with your projects. :-)
Maverick_CoolAuthor Commented:
I got the partial results.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.