Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello i want to hide and show columns in gridview based on checkbox i done half the job but i face problem, i used javascript and the all var value can hide or show good but when i use number value int or sth else this doesn't work teh problem with total column taht has number value i think the var declaration is the problem some1help

What I have tried:

<script type="text/javascript">
    $(function () {
        $("[id*=chkCountry]").click(function () {
            var isChecked = $(this).is(":checked");
            var th = $("[id*=GridView1] th:contains('Country')");
            th.css("display", isChecked ? "" : "none");
            $("[id*=GridView1] tr").each(function () {
                $(this).find("td").eq(th.index()).css("display", isChecked ? "" : "none");
            });
        });
    });
    $(function () {
        $("[id*=Name]").click(function () {
            var isChecked = $(this).is(":checked");
            var th = $("[id*=GridView1] th:contains('Name')");
            th.css("display", isChecked ? "" : "none");
            $("[id*=GridView1] tr").each(function () {
                $(this).find("td").eq(th.index()).css("display", isChecked ? "" : "none");
            });
        });
    });

    $(function () {
        $("[id*=chktotal]").click(function () {
            var isChecked = $(this).is(":checked");
            var th = $("[id*=GridView1] th:contains('Total')");
            th.css("display", isChecked ? "" : "none");
            $("[id*=GridView1] tr").each(function () {
                $(this).find("td").eq(th.index()).css("display", isChecked ? "" : "none");
            });
        });
    });
Posted
Updated 20-Mar-17 21:54pm
Comments
Afzaal Ahmad Zeeshan 20-Mar-17 14:25pm    
If you are using jQuery, why don't you start using the jQuery UI? It has some excellent controls available that you can implement — controls which already have these features.
Karthik_Mahalingam 21-Mar-17 1:30am    
why not hide() instead of css("display", isChecked ? "" : "none");
Member 13044689 21-Mar-17 2:14am    
ok if u recommend another method to hide and show column based on checkbox in gridview u can give me example, i will be appreciate
Karthik_Mahalingam 21-Mar-17 2:30am    
post the relevant code for the same
mark up
Member 13044689 21-Mar-17 3:01am    
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
CellPadding="4" DataKeyNames="Id" DataSourceID="SqlDataSource1">
<columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True"
SortExpression="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
<asp:BoundField DataField="purchesvalue" HeaderText="purchesvalue"
SortExpression="purchesvalue" />
<asp:BoundField DataField="shippingvalue" HeaderText="shippingvalue"
SortExpression="shippingvalue" />
<asp:BoundField DataField="othercharge" HeaderText="othercharge"
SortExpression="othercharge" />
<asp:BoundField DataField="Total" HeaderText="Total" SortExpression="Total" />

<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:All_VechConnectionString1 %>"
SelectCommand="SELECT * FROM [test]">






((( this is my table can u plz help me with this))

1 solution

refer this example

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
        $(function () {
            $('.chkcls').change(function (a, b) {
                var isChecked = a.target.checked;
                var grid = document.getElementById('<%= gv.ClientID%>');
                var target = $(this).attr('column-control');
                var targetTH = $("th:contains('" + target + "')", grid);
                var index = $('th', grid).index(targetTH);
                $('tr', grid).each(function (i, row) { $('td:nth(' + index + ')', row).css("display", isChecked ? "" : "none"); });
                targetTH.css("display", isChecked ? "" : "none");


            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">

        <asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True"
                    SortExpression="Id" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Country" HeaderText="Country"
                    SortExpression="Country" />

            </Columns>
        </asp:GridView>


        <asp:CheckBox AutoPostBack="false" Checked="true" column-control="Id" CssClass="chkcls" runat="server" ID="chkid" Text="show hide id" />
        <asp:CheckBox AutoPostBack="false" Checked="true" column-control="Name" CssClass="chkcls" runat="server" ID="chkname" Text="show hide name" />
        <asp:CheckBox AutoPostBack="false" Checked="true" column-control="Country" CssClass="chkcls" runat="server" ID="chkcountry" Text="show hide country" />

    </form>
</body>
</html>




protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               DataTable dt = new DataTable();
               dt.Columns.Add("ID");
               dt.Columns.Add("Name");
               dt.Columns.Add("Country");
               dt.Rows.Add(1, "AA", "India");
               dt.Rows.Add(2, "bb", "US");
               dt.Rows.Add(3, "cc", "UK");
               gv.DataSource = dt;
               gv.DataBind();
           }


       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900