Preserving State of Show Hide Column

Last Reply 3 days ago By dharmendr

Posted 9 days ago

As You Saw  I hide the column in the gridview of Section A. After That I select the Section-B and Now it is showing all the column of Section B as per code. This is working fine. Now I am back and again select the Section A. Now at this point it should not show  all the column  as some column were hidden by check off the check box before selection of section B. Simply I want the preserving state of Section A when its column was hidden. It should not show the all column in the gridview until I want and again I checked on the check box.

This is working fine. Now that My requirement is that after selection Section-B when I again select Sectin-A, 

https://imgur.com/G58Otmz 

<script src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=chkAdNo]").click(function () {
var isChecked = $(this).is(":checked");
var th = $("[id*=gvStudents] th:contains('Reg No')");
th.css("display", isChecked ? "" : "none");
$("[id*=gvStudents] tr").each(function () {
$(this).find("td").eq(th.index()).css("display", isChecked ? "" : "none");
});
});
});


$(function () {
$("[id*=chkRefNo]").click(function () {
var isChecked = $(this).is(":checked");
var th = $("[id*=gvStudents] th:contains('Ref No')");
th.css("display", isChecked ? "" : "none");
$("[id*=gvStudents] tr").each(function () {
$(this).find("td").eq(th.index()).css("display", isChecked ? "" : "none");
});
});
});

$(function () {
$("[id*=chkPId]").click(function () {
var isChecked = $(this).is(":checked");
var th = $("[id*=gvStudents] th:contains('F Code')");
th.css("display", isChecked ? "" : "none");
$("[id*=gvStudents] tr").each(function () {
$(this).find("td").eq(th.index()).css("display", isChecked ? "" : "none");
});
});
});

$(function () {
$("[id*=chkAD]").click(function () {
var isChecked = $(this).is(":checked");
var th = $("[id*=gvStudents] th:contains('Adm Date')");
th.css("display", isChecked ? "" : "none");
$("[id*=gvStudents] tr").each(function () {
$(this).find("td").eq(th.index()).css("display", isChecked ? "" : "none");
});
});
});


$(function () {
$("[id*=chkRN]").click(function () {
var isChecked = $(this).is(":checked");
var th = $("[id*=gvStudents] th:contains('R No')");
th.css("display", isChecked ? "" : "none");
$("[id*=gvStudents] tr").each(function () {
$(this).find("td").eq(th.index()).css("display", isChecked ? "" : "none");
});
});
});


</script>

 

<asp:CheckBox ID="chkAdNo" Text="Reg No" runat="server" Checked="true" />

<asp:CheckBox ID="chkRefNo" Text="Ref No" runat="server" Checked="true" />
<asp:CheckBox ID="chkPId" Text="F Code" runat="server" Checked="true" />
<asp:CheckBox ID="chkAD" Text="Adm Date" runat="server" Checked="true" />
<asp:CheckBox ID="chkRN" Text="R No" runat="server" Checked="true" />
<asp:CheckBox ID="chkSN" Text="SName" runat="server" Checked="true" />


<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="false" OnRowDataBound = "OnRowDataBound"
DataKeyNames = "StuID" Class="table table-striped table-bordered table-hover" OnDataBound="OnDataBound"
AllowPaging="false" OnPageIndexChanging="OnPaging" RowStyle-Wrap="false" HeaderStyle-Wrap="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID = "chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Stu ID" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label31" runat="server" Text='<%# Eval("StuID") %>'></asp:Label>
<asp:TextBox ID="TextBox31" runat="server" Text='<%# Eval("StuID") %>' Visible="false" class="form-control"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Reg No" ItemStyle-Width = "250">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("AdmissionNo") %>'></asp:Label>
<asp:TextBox ID="TextBox32" runat="server" Text='<%# Eval("AdmissionNo") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Ref No" ItemStyle-Width = "250">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("ReferenceNo") %>'></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("ReferenceNo") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="F Code" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Eval("ParentID") %>'></asp:Label>
<asp:TextBox ID="TextBox12" runat="server" Text='<%# Eval("ParentID") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Adm Date" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("AdmissionDate") %>'></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("AdmissionDate") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="SName" ItemStyle-Width = "250">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("SName") %>'></asp:Label>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Eval("SName") %>' Visible="false" class="form-control" Width="250"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="R No" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("RollNo") %>'></asp:Label>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("RollNo") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="DOB" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("DOB") %>'></asp:Label>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Eval("DOB") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="SCNIC" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Eval("SCNIC") %>'></asp:Label>
<asp:TextBox ID="TextBox10" runat="server" Text='<%# Eval("SCNIC") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>


<asp:TemplateField HeaderText="FName" ItemStyle-Width = "250">
<ItemTemplate>
<asp:Label ID="Label14" runat="server" Text='<%# Eval("FName") %>'></asp:Label>
<asp:TextBox ID="TextBox14" runat="server" Text='<%# Eval("FName") %>' Visible="false" class="form-control" Width="250"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="FCNIC" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label13" runat="server" Text='<%# Eval("FCNIC") %>'></asp:Label>
<asp:TextBox ID="TextBox13" runat="server" Text='<%# Eval("FCNIC") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="LevRem" ItemStyle-Width = "250">
<ItemTemplate>
<asp:Label ID="Label35" runat="server" Text='<%# Eval("LeavingRemarks") %>'></asp:Label>
<asp:TextBox ID="TextBox35" runat="server" Text='<%# Eval("LeavingRemarks") %>' Visible="false" class="form-control" Width="250"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>


<asp:TemplateField HeaderText="Gender" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("Gender") %>'></asp:Label>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Eval("Gender") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Rel" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Eval("Religion") %>'></asp:Label>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Eval("Religion") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="BG" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Eval("BloodGroup") %>'></asp:Label>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Eval("BloodGroup") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="SPhone" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Eval("SPhone") %>'></asp:Label>
<asp:TextBox ID="TextBox11" runat="server" Text='<%# Eval("SPhone") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>


<asp:TemplateField HeaderText="FPhone" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label15" runat="server" Text='<%# Eval("FPhone") %>'></asp:Label>
<asp:TextBox ID="TextBox15" runat="server" Text='<%# Eval("FPhone") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="MPhone" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label19" runat="server" Text='<%# Eval("MPhone") %>'></asp:Label>
<asp:TextBox ID="TextBox19" runat="server" Text='<%# Eval("MPhone") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="GPhone" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label24" runat="server" Text='<%# Eval("GPhone") %>'></asp:Label>
<asp:TextBox ID="TextBox24" runat="server" Text='<%# Eval("GPhone") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="FJob" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label16" runat="server" Text='<%# Eval("FJob") %>'></asp:Label>
<asp:TextBox ID="TextBox16" runat="server" Text='<%# Eval("FJob") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>


<asp:TemplateField HeaderText="MName" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label17" runat="server" Text='<%# Eval("MName") %>'></asp:Label>
<asp:TextBox ID="TextBox17" runat="server" Text='<%# Eval("MName") %>' Visible="false" class="form-control" Width="250"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="MCNIC" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label18" runat="server" Text='<%# Eval("MCNIC") %>'></asp:Label>
<asp:TextBox ID="TextBox18" runat="server" Text='<%# Eval("MCNIC") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="MJob" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label20" runat="server" Text='<%# Eval("MJob") %>'></asp:Label>
<asp:TextBox ID="TextBox20" runat="server" Text='<%# Eval("MJob") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Add" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label21" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
<asp:TextBox ID="TextBox21" runat="server" Text='<%# Eval("Address") %>' Visible="false" class="form-control" Width="250"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>


<asp:TemplateField HeaderText="GName" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label23" runat="server" Text='<%# Eval("GuardianName") %>'></asp:Label>
<asp:TextBox ID="TextBox23" runat="server" Text='<%# Eval("GuardianName") %>' Visible="false" class="form-control" Width="250"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="GCNIC" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label28" runat="server" Text='<%# Eval("GCNIC") %>'></asp:Label>
<asp:TextBox ID="TextBox28" runat="server" Text='<%# Eval("GCNIC") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="GAdd" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label29" runat="server" Text='<%# Eval("GAddress") %>'></asp:Label>
<asp:TextBox ID="TextBox29" runat="server" Text='<%# Eval("GAddress") %>' Visible="false" class="form-control" Width="250"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qual" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label25" runat="server" Text='<%# Eval("Qualification") %>'></asp:Label>
<asp:TextBox ID="TextBox25" runat="server" Text='<%# Eval("Qualification") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Income" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label26" runat="server" Text='<%# Eval("Income") %>'></asp:Label>
<asp:TextBox ID="TextBox26" runat="server" Text='<%# Eval("Income") %>' Visible="false" class="form-control" Width="150"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label27" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
<asp:TextBox ID="TextBox27" runat="server" Text='<%# Eval("Email") %>' Visible="false" class="form-control" Width="250"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Active" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID="Label128" runat="server" Text='<%# Eval("Active_Status") %>'></asp:Label>
<asp:TextBox ID="TextBox128" runat="server" Text='<%# Eval("Active_Status") %>' Visible="false" class="form-control"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>


</Columns>
</asp:GridView>
You are viewing reply posted by: dharmendr 8 days ago.
Posted 8 days ago Modified on 8 days ago

Hi smile,

Check this example. Now please take its reference and correct your code.

In order to maintain state of CheckBox selection for the particular section you have to use ViewState for maintaining the selection.

HTML

<div>
    <asp:DropDownList ID="ddlSection" runat="server" OnSelectedIndexChanged="Changed"
        AutoPostBack="true">
        <asp:ListItem Text="Select" />
        <asp:ListItem Text="A" Value="A" />
        <asp:ListItem Text="B" Value="B" />
    </asp:DropDownList>
    <br />
    <asp:Panel runat="server" ID="pnlCheckBoxes">
        <asp:CheckBox OnCheckedChanged="Filter" ID="chkAdNo" Text="Reg No" runat="server"
            AutoPostBack="true" Checked="true" />
        <asp:CheckBox OnCheckedChanged="Filter" ID="chkRefNo" Text="Ref No" runat="server"
            AutoPostBack="true" Checked="true" />
        <asp:CheckBox OnCheckedChanged="Filter" ID="chkPId" Text="F Code" runat="server"
            AutoPostBack="true" Checked="true" />
        <asp:CheckBox OnCheckedChanged="Filter" ID="chkAD" Text="Adm Date" runat="server"
            AutoPostBack="true" Checked="true" />
        <asp:CheckBox OnCheckedChanged="Filter" ID="chkRN" Text="R No" runat="server" Checked="true"
            AutoPostBack="true" />
        <asp:CheckBox OnCheckedChanged="Filter" ID="chkSN" Text="SName" runat="server" Checked="true"
            AutoPostBack="true" />
    </asp:Panel>
    <br />
    <asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="false" DataKeyNames="StuID"
        Class="table table-striped table-bordered table-hover" AllowPaging="false" RowStyle-Wrap="false"
        HeaderStyle-Wrap="false">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="chkAll" runat="server" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Stu ID" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label31" runat="server" Text='<%# Eval("StuID") %>'></asp:Label>
                    <asp:TextBox ID="TextBox31" runat="server" Text='<%# Eval("StuID") %>' Visible="false"
                        class="form-control"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Reg No" ItemStyle-Width="250">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("AdmissionNo") %>'></asp:Label>
                    <asp:TextBox ID="TextBox32" runat="server" Text='<%# Eval("AdmissionNo") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Ref No" ItemStyle-Width="250">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("ReferenceNo") %>'></asp:Label>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("ReferenceNo") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="F Code" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label12" runat="server" Text='<%# Eval("ParentID") %>'></asp:Label>
                    <asp:TextBox ID="TextBox12" runat="server" Text='<%# Eval("ParentID") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Adm Date" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("AdmissionDate") %>'></asp:Label>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("AdmissionDate") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="SName" ItemStyle-Width="250">
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Eval("SName") %>'></asp:Label>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Eval("SName") %>' Visible="false"
                        class="form-control" Width="250"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="R No" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Eval("RollNo") %>'></asp:Label>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("RollNo") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="DOB" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label6" runat="server" Text='<%# Eval("DOB") %>'></asp:Label>
                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Eval("DOB") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="SCNIC" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label10" runat="server" Text='<%# Eval("SCNIC") %>'></asp:Label>
                    <asp:TextBox ID="TextBox10" runat="server" Text='<%# Eval("SCNIC") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="FName" ItemStyle-Width="250">
                <ItemTemplate>
                    <asp:Label ID="Label14" runat="server" Text='<%# Eval("FName") %>'></asp:Label>
                    <asp:TextBox ID="TextBox14" runat="server" Text='<%# Eval("FName") %>' Visible="false"
                        class="form-control" Width="250"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="FCNIC" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label13" runat="server" Text='<%# Eval("FCNIC") %>'></asp:Label>
                    <asp:TextBox ID="TextBox13" runat="server" Text='<%# Eval("FCNIC") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="LevRem" ItemStyle-Width="250">
                <ItemTemplate>
                    <asp:Label ID="Label35" runat="server" Text='<%# Eval("LeavingRemarks") %>'></asp:Label>
                    <asp:TextBox ID="TextBox35" runat="server" Text='<%# Eval("LeavingRemarks") %>' Visible="false"
                        class="form-control" Width="250"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Gender" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Eval("Gender") %>'></asp:Label>
                    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Eval("Gender") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Rel" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label9" runat="server" Text='<%# Eval("Religion") %>'></asp:Label>
                    <asp:TextBox ID="TextBox9" runat="server" Text='<%# Eval("Religion") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="BG" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label8" runat="server" Text='<%# Eval("BloodGroup") %>'></asp:Label>
                    <asp:TextBox ID="TextBox8" runat="server" Text='<%# Eval("BloodGroup") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="SPhone" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label11" runat="server" Text='<%# Eval("SPhone") %>'></asp:Label>
                    <asp:TextBox ID="TextBox11" runat="server" Text='<%# Eval("SPhone") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="FPhone" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label15" runat="server" Text='<%# Eval("FPhone") %>'></asp:Label>
                    <asp:TextBox ID="TextBox15" runat="server" Text='<%# Eval("FPhone") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="MPhone" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label19" runat="server" Text='<%# Eval("MPhone") %>'></asp:Label>
                    <asp:TextBox ID="TextBox19" runat="server" Text='<%# Eval("MPhone") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="GPhone" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label24" runat="server" Text='<%# Eval("GPhone") %>'></asp:Label>
                    <asp:TextBox ID="TextBox24" runat="server" Text='<%# Eval("GPhone") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="FJob" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label16" runat="server" Text='<%# Eval("FJob") %>'></asp:Label>
                    <asp:TextBox ID="TextBox16" runat="server" Text='<%# Eval("FJob") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="MName" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label17" runat="server" Text='<%# Eval("MName") %>'></asp:Label>
                    <asp:TextBox ID="TextBox17" runat="server" Text='<%# Eval("MName") %>' Visible="false"
                        class="form-control" Width="250"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="MCNIC" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label18" runat="server" Text='<%# Eval("MCNIC") %>'></asp:Label>
                    <asp:TextBox ID="TextBox18" runat="server" Text='<%# Eval("MCNIC") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="MJob" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label20" runat="server" Text='<%# Eval("MJob") %>'></asp:Label>
                    <asp:TextBox ID="TextBox20" runat="server" Text='<%# Eval("MJob") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Add" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label21" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
                    <asp:TextBox ID="TextBox21" runat="server" Text='<%# Eval("Address") %>' Visible="false"
                        class="form-control" Width="250"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="GName" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label23" runat="server" Text='<%# Eval("GuardianName") %>'></asp:Label>
                    <asp:TextBox ID="TextBox23" runat="server" Text='<%# Eval("GuardianName") %>' Visible="false"
                        class="form-control" Width="250"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="GCNIC" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label28" runat="server" Text='<%# Eval("GCNIC") %>'></asp:Label>
                    <asp:TextBox ID="TextBox28" runat="server" Text='<%# Eval("GCNIC") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="GAdd" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label29" runat="server" Text='<%# Eval("GAddress") %>'></asp:Label>
                    <asp:TextBox ID="TextBox29" runat="server" Text='<%# Eval("GAddress") %>' Visible="false"
                        class="form-control" Width="250"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Qual" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label25" runat="server" Text='<%# Eval("Qualification") %>'></asp:Label>
                    <asp:TextBox ID="TextBox25" runat="server" Text='<%# Eval("Qualification") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Income" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label26" runat="server" Text='<%# Eval("Income") %>'></asp:Label>
                    <asp:TextBox ID="TextBox26" runat="server" Text='<%# Eval("Income") %>' Visible="false"
                        class="form-control" Width="150"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Email" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label27" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
                    <asp:TextBox ID="TextBox27" runat="server" Text='<%# Eval("Email") %>' Visible="false"
                        class="form-control" Width="250"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Active" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="Label128" runat="server" Text='<%# Eval("Active_Status") %>'></asp:Label>
                    <asp:TextBox ID="TextBox128" runat="server" Text='<%# Eval("Active_Status") %>' Visible="false"
                        class="form-control"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>

Namespace

C#

using System.Data;

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        BindGrid();
    }
}

private void BindGrid()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("StuID", typeof(int)),
                        new DataColumn("AdmissionNo", typeof(string)),
                        new DataColumn("ReferenceNo",typeof(string)),
                        new DataColumn("ParentID", typeof(string)),
                        new DataColumn("AdmissionDate",typeof(string)),
                        new DataColumn("SName", typeof(string)),
                        new DataColumn("RollNo",typeof(string)),
                        new DataColumn("DOB", typeof(string)),
                        new DataColumn("SCNIC",typeof(string)),
                        new DataColumn("FName", typeof(string)),
                        new DataColumn("FCNIC",typeof(string)),
                        new DataColumn("LeavingRemarks", typeof(string)),
                        new DataColumn("Gender",typeof(string)),
                        new DataColumn("Religion", typeof(string)),
                        new DataColumn("BloodGroup",typeof(string)),
                        new DataColumn("SPhone", typeof(string)),
                        new DataColumn("FPhone",typeof(string)),
                        new DataColumn("MPhone", typeof(string)),
                        new DataColumn("GPhone",typeof(string)),
                        new DataColumn("FJob", typeof(string)),
                        new DataColumn("MName",typeof(string)),
                        new DataColumn("MCNIC", typeof(string)),
                        new DataColumn("MJob",typeof(string)),
                        new DataColumn("Address", typeof(string)),
                        new DataColumn("GuardianName",typeof(string)),
                        new DataColumn("GCNIC", typeof(string)),
                        new DataColumn("GAddress",typeof(string)),
                        new DataColumn("Qualification", typeof(string)),
                        new DataColumn("Income", typeof(string)),
                        new DataColumn("Email", typeof(string)),
                        new DataColumn("Active_Status", typeof(string))
        });
    dt.Rows.Add(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
    gvStudents.DataSource = dt;
    gvStudents.DataBind();
}

protected void Changed(object sender, EventArgs e)
{
    BindGrid();
    foreach (DataControlField col in gvStudents.Columns)
    {
        col.Visible = true;
    }
    foreach (Control c in pnlCheckBoxes.Controls)
    {
        if ((c is CheckBox))
        {
            ((CheckBox)c).Checked = true;
        }
    }
    if (ViewState["items"] != null)
    {
        List<CheckBoxSection> items = (List<CheckBoxSection>)ViewState["items"];
        for (int i = 0; i < items.Count; i++)
        {
            if (items[i].Section == ddlSection.SelectedValue)
            {
                string checkboxes = items[i].Check;
                for (int j = 0; j < checkboxes.Split(',').Length; j++)
                {
                    if (!string.IsNullOrEmpty(checkboxes.Split(',')[j].Trim()))
                    {
                        string checkboxId = checkboxes.Split(',')[j];
                        foreach (Control c in pnlCheckBoxes.Controls)
                        {
                            if ((c is CheckBox))
                            {
                                if (c.UniqueID.Trim() == checkboxId.Trim())
                                {
                                    ((CheckBox)c).Checked = false;
                                    foreach (DataControlField col in gvStudents.Columns)
                                    {
                                        if (col.HeaderText == ((CheckBox)c).Text)
                                        {
                                            col.Visible = false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

protected void Filter(object sender, EventArgs e)
{
    foreach (DataControlField col in gvStudents.Columns)
    {
        if (col.HeaderText == (sender as CheckBox).Text)
        {
            if (!(sender as CheckBox).Checked)
            {
                col.Visible = false;
            }
            else
            {
                col.Visible = true;
            }
        }
    }
    if (ViewState["items"] == null)
    {
        List<CheckBoxSection> items = new List<CheckBoxSection>();
        string str = "";
        foreach (Control c in pnlCheckBoxes.Controls)
        {
            if ((c is CheckBox) && !((CheckBox)c).Checked)
            {
                str += c.UniqueID + ", ";
            }
        }

        items.Add(new CheckBoxSection { Section = ddlSection.SelectedValue, Check = str });
        ViewState["items"] = items;
    }
    else
    {
        List<CheckBoxSection> items = (List<CheckBoxSection>)ViewState["items"];
        string str = "";
        foreach (Control c in pnlCheckBoxes.Controls)
        {
            if ((c is CheckBox) && !((CheckBox)c).Checked)
            {
                str += c.UniqueID + ", ";
            }
        }
        bool isExist = true;
        for (int i = 0; i < items.Count; i++)
        {
            if (items[i].Section == ddlSection.SelectedValue)
            {
                items[i].Check = str;
            }
            else
            {
                isExist = false;
            }
        }
        if (!isExist)
        {
            items.Add(new CheckBoxSection { Section = ddlSection.SelectedValue, Check = str });
        }
        ViewState["items"] = items;
    }
}

[Serializable]
public class CheckBoxSection
{
    public string Section { get; set; }
    public string Check { get; set; }
}

Screenshot