Link to home
Start Free TrialLog in
Avatar of Maverick_Cool
Maverick_CoolFlag for India

asked on

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

Hi,

  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.



Avatar of Alfred A.
Alfred A.
Flag of Australia image

Inside your ASP.NET Source do this

Javascript:

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

            xState=theBox.checked;
            elm=theBox.form.elements;
            for(i=0;i<elm.length;i++)
            if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)
            {
            //elm[i].click();
            if(elm[i].checked!=xState)
            elm[i].click();
            //elm[i].checked=xState;
            }
            }
    </script>

//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" />
        <Columns>

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


I hope this helps.
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.

ASKER CERTIFIED SOLUTION
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Maverick_Cool

ASKER

Hi Alfred1,

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




Glad to help.  :-)

Goodluck with your projects. :-)
I got the partial results.