Preserving State of show hide ASP.Net GridView Columns using C# and VB.Net

Last Reply 3 months ago By dharmendr

Posted 3 months 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

Posted 3 months ago Modified on 3 months 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;

VB.Net

Imports 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; }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        BindGrid()
    End If
End Sub

Private Sub BindGrid()
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {
                        New DataColumn("StuID", GetType(Integer)), New DataColumn("AdmissionNo", GetType(String)),
                        New DataColumn("ReferenceNo", GetType(String)), New DataColumn("ParentID", GetType(String)),
                        New DataColumn("AdmissionDate", GetType(String)), New DataColumn("SName", GetType(String)),
                        New DataColumn("RollNo", GetType(String)), New DataColumn("DOB", GetType(String)),
                        New DataColumn("SCNIC", GetType(String)), New DataColumn("FName", GetType(String)),
                        New DataColumn("FCNIC", GetType(String)), New DataColumn("LeavingRemarks", GetType(String)),
                        New DataColumn("Gender", GetType(String)), New DataColumn("Religion", GetType(String)),
                        New DataColumn("BloodGroup", GetType(String)), New DataColumn("SPhone", GetType(String)),
                        New DataColumn("FPhone", GetType(String)), New DataColumn("MPhone", GetType(String)),
                        New DataColumn("GPhone", GetType(String)), New DataColumn("FJob", GetType(String)),
                        New DataColumn("MName", GetType(String)), New DataColumn("MCNIC", GetType(String)),
                        New DataColumn("MJob", GetType(String)), New DataColumn("Address", GetType(String)),
                        New DataColumn("GuardianName", GetType(String)),
                        New DataColumn("GCNIC", GetType(String)), New DataColumn("GAddress", GetType(String)),
                        New DataColumn("Qualification", GetType(String)), New DataColumn("Income", GetType(String)),
                        New DataColumn("Email", GetType(String)), New DataColumn("Active_Status", GetType(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()
End Sub

Protected Sub Changed(ByVal sender As Object, ByVal e As EventArgs)
    BindGrid()
    For Each col As DataControlField In gvStudents.Columns
        col.Visible = True
    Next
    For Each c As Control In pnlCheckBoxes.Controls
        If (TypeOf c Is CheckBox) Then
            CType(c, CheckBox).Checked = True
        End If
    Next
    If ViewState("items") IsNot Nothing Then
        Dim items As List(Of CheckBoxSection) = CType(ViewState("items"), List(Of CheckBoxSection))
        For i As Integer = 0 To items.Count - 1
            If items(i).Section = ddlSection.SelectedValue Then
                Dim checkboxes As String = items(i).Check
                For j As Integer = 0 To checkboxes.Split(","c).Length - 1
                    If Not String.IsNullOrEmpty(checkboxes.Split(","c)(j).Trim()) Then
                        Dim checkboxId As String = checkboxes.Split(","c)(j)
                        For Each c As Control In pnlCheckBoxes.Controls
                            If (TypeOf c Is CheckBox) Then
                                If c.UniqueID.Trim() = checkboxId.Trim() Then
                                    CType(c, CheckBox).Checked = False
                                    For Each col As DataControlField In gvStudents.Columns
                                        If col.HeaderText = (CType(c, CheckBox)).Text Then
                                            col.Visible = False
                                        End If
                                    Next
                                End If
                            End If
                        Next
                    End If
                Next
            End If
        Next
    End If
End Sub

Protected Sub Filter(ByVal sender As Object, ByVal e As EventArgs)
    For Each col As DataControlField In gvStudents.Columns
        If col.HeaderText = (TryCast(sender, CheckBox)).Text Then
            If Not (TryCast(sender, CheckBox)).Checked Then
                col.Visible = False
            Else
                col.Visible = True
            End If
        End If
    Next

    If ViewState("items") Is Nothing Then
        Dim items As List(Of CheckBoxSection) = New List(Of CheckBoxSection)()
        Dim str As String = ""
        For Each c As Control In pnlCheckBoxes.Controls
            If (TypeOf c Is CheckBox) AndAlso Not (CType(c, CheckBox)).Checked Then
                str += c.UniqueID & ", "
            End If
        Next
        items.Add(New CheckBoxSection With {
            .Section = ddlSection.SelectedValue,
            .Check = str
        })
        ViewState("items") = items
    Else
        Dim items As List(Of CheckBoxSection) = CType(ViewState("items"), List(Of CheckBoxSection))
        Dim str As String = ""
        For Each c As Control In pnlCheckBoxes.Controls
            If (TypeOf c Is CheckBox) AndAlso Not (CType(c, CheckBox)).Checked Then
                str += c.UniqueID & ", "
            End If
        Next
        Dim isExist As Boolean = True
        For i As Integer = 0 To items.Count - 1
            If items(i).Section = ddlSection.SelectedValue Then
                items(i).Check = str
            Else
                isExist = False
            End If
        Next
        If Not isExist Then
            items.Add(New CheckBoxSection With {
                .Section = ddlSection.SelectedValue,
                .Check = str
            })
        End If
        ViewState("items") = items
    End If
End Sub

<Serializable()>
Public Class CheckBoxSection
    Public Property Section As String
    Public Property Check As String
End Class

Screenshot