ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Preserving State of show hide ASP.Net GridView Columns using C# and VB.Net<p>Hi&nbsp;smile,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p>In order to maintain state of CheckBox selection for the particular section you have to use ViewState for maintaining the selection.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;div&gt; &lt;asp:DropDownList ID=&#34;ddlSection&#34; runat=&#34;server&#34; OnSelectedIndexChanged=&#34;Changed&#34; AutoPostBack=&#34;true&#34;&gt; &lt;asp:ListItem Text=&#34;Select&#34; /&gt; &lt;asp:ListItem Text=&#34;A&#34; Value=&#34;A&#34; /&gt; &lt;asp:ListItem Text=&#34;B&#34; Value=&#34;B&#34; /&gt; &lt;/asp:DropDownList&gt; &lt;br /&gt; &lt;asp:Panel runat=&#34;server&#34; ID=&#34;pnlCheckBoxes&#34;&gt; &lt;asp:CheckBox OnCheckedChanged=&#34;Filter&#34; ID=&#34;chkAdNo&#34; Text=&#34;Reg No&#34; runat=&#34;server&#34; AutoPostBack=&#34;true&#34; Checked=&#34;true&#34; /&gt; &lt;asp:CheckBox OnCheckedChanged=&#34;Filter&#34; ID=&#34;chkRefNo&#34; Text=&#34;Ref No&#34; runat=&#34;server&#34; AutoPostBack=&#34;true&#34; Checked=&#34;true&#34; /&gt; &lt;asp:CheckBox OnCheckedChanged=&#34;Filter&#34; ID=&#34;chkPId&#34; Text=&#34;F Code&#34; runat=&#34;server&#34; AutoPostBack=&#34;true&#34; Checked=&#34;true&#34; /&gt; &lt;asp:CheckBox OnCheckedChanged=&#34;Filter&#34; ID=&#34;chkAD&#34; Text=&#34;Adm Date&#34; runat=&#34;server&#34; AutoPostBack=&#34;true&#34; Checked=&#34;true&#34; /&gt; &lt;asp:CheckBox OnCheckedChanged=&#34;Filter&#34; ID=&#34;chkRN&#34; Text=&#34;R No&#34; runat=&#34;server&#34; Checked=&#34;true&#34; AutoPostBack=&#34;true&#34; /&gt; &lt;asp:CheckBox OnCheckedChanged=&#34;Filter&#34; ID=&#34;chkSN&#34; Text=&#34;SName&#34; runat=&#34;server&#34; Checked=&#34;true&#34; AutoPostBack=&#34;true&#34; /&gt; &lt;/asp:Panel&gt; &lt;br /&gt; &lt;asp:GridView ID=&#34;gvStudents&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; DataKeyNames=&#34;StuID&#34; Class=&#34;table table-striped table-bordered table-hover&#34; AllowPaging=&#34;false&#34; RowStyle-Wrap=&#34;false&#34; HeaderStyle-Wrap=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField&gt; &lt;HeaderTemplate&gt; &lt;asp:CheckBox ID=&#34;chkAll&#34; runat=&#34;server&#34; /&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&#34;CheckBox1&#34; runat=&#34;server&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Stu ID&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label31&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;StuID&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox31&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;StuID&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Reg No&#34; ItemStyle-Width=&#34;250&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label1&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;AdmissionNo&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox32&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;AdmissionNo&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Ref No&#34; ItemStyle-Width=&#34;250&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label2&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;ReferenceNo&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox2&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;ReferenceNo&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;F Code&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label12&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;ParentID&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox12&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;ParentID&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Adm Date&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label3&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;AdmissionDate&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox3&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;AdmissionDate&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;SName&#34; ItemStyle-Width=&#34;250&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label5&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;SName&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox5&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;SName&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;250&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;R No&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label4&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;RollNo&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox4&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;RollNo&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;DOB&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label6&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;DOB&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox6&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;DOB&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;SCNIC&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label10&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;SCNIC&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox10&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;SCNIC&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;FName&#34; ItemStyle-Width=&#34;250&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label14&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;FName&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox14&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;FName&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;250&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;FCNIC&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label13&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;FCNIC&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox13&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;FCNIC&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;LevRem&#34; ItemStyle-Width=&#34;250&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label35&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;LeavingRemarks&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox35&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;LeavingRemarks&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;250&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Gender&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label7&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Gender&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox7&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Gender&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Rel&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label9&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Religion&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox9&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Religion&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;BG&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label8&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;BloodGroup&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox8&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;BloodGroup&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;SPhone&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label11&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;SPhone&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox11&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;SPhone&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;FPhone&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label15&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;FPhone&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox15&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;FPhone&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;MPhone&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label19&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;MPhone&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox19&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;MPhone&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;GPhone&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label24&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;GPhone&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox24&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;GPhone&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;FJob&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label16&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;FJob&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox16&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;FJob&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;MName&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label17&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;MName&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox17&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;MName&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;250&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;MCNIC&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label18&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;MCNIC&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox18&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;MCNIC&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;MJob&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label20&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;MJob&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox20&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;MJob&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Add&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label21&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Address&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox21&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Address&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;250&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;GName&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label23&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;GuardianName&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox23&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;GuardianName&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;250&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;GCNIC&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label28&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;GCNIC&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox28&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;GCNIC&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;GAdd&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label29&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;GAddress&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox29&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;GAddress&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;250&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Qual&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label25&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Qualification&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox25&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Qualification&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Income&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label26&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Income&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox26&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Income&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;150&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Email&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label27&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Email&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox27&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Email&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34; Width=&#34;250&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Active&#34; ItemStyle-Width=&#34;150&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label128&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Active_Status&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextBox128&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Active_Status&#34;) %&gt;&#39; Visible=&#34;false&#34; class=&#34;form-control&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/div&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespace</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">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(&#34;StuID&#34;, typeof(int)), new DataColumn(&#34;AdmissionNo&#34;, typeof(string)), new DataColumn(&#34;ReferenceNo&#34;,typeof(string)), new DataColumn(&#34;ParentID&#34;, typeof(string)), new DataColumn(&#34;AdmissionDate&#34;,typeof(string)), new DataColumn(&#34;SName&#34;, typeof(string)), new DataColumn(&#34;RollNo&#34;,typeof(string)), new DataColumn(&#34;DOB&#34;, typeof(string)), new DataColumn(&#34;SCNIC&#34;,typeof(string)), new DataColumn(&#34;FName&#34;, typeof(string)), new DataColumn(&#34;FCNIC&#34;,typeof(string)), new DataColumn(&#34;LeavingRemarks&#34;, typeof(string)), new DataColumn(&#34;Gender&#34;,typeof(string)), new DataColumn(&#34;Religion&#34;, typeof(string)), new DataColumn(&#34;BloodGroup&#34;,typeof(string)), new DataColumn(&#34;SPhone&#34;, typeof(string)), new DataColumn(&#34;FPhone&#34;,typeof(string)), new DataColumn(&#34;MPhone&#34;, typeof(string)), new DataColumn(&#34;GPhone&#34;,typeof(string)), new DataColumn(&#34;FJob&#34;, typeof(string)), new DataColumn(&#34;MName&#34;,typeof(string)), new DataColumn(&#34;MCNIC&#34;, typeof(string)), new DataColumn(&#34;MJob&#34;,typeof(string)), new DataColumn(&#34;Address&#34;, typeof(string)), new DataColumn(&#34;GuardianName&#34;,typeof(string)), new DataColumn(&#34;GCNIC&#34;, typeof(string)), new DataColumn(&#34;GAddress&#34;,typeof(string)), new DataColumn(&#34;Qualification&#34;, typeof(string)), new DataColumn(&#34;Income&#34;, typeof(string)), new DataColumn(&#34;Email&#34;, typeof(string)), new DataColumn(&#34;Active_Status&#34;, 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[&#34;items&#34;] != null) { List&lt;CheckBoxSection&gt; items = (List&lt;CheckBoxSection&gt;)ViewState[&#34;items&#34;]; for (int i = 0; i &lt; items.Count; i++) { if (items[i].Section == ddlSection.SelectedValue) { string checkboxes = items[i].Check; for (int j = 0; j &lt; checkboxes.Split(&#39;,&#39;).Length; j++) { if (!string.IsNullOrEmpty(checkboxes.Split(&#39;,&#39;)[j].Trim())) { string checkboxId = checkboxes.Split(&#39;,&#39;)[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[&#34;items&#34;] == null) { List&lt;CheckBoxSection&gt; items = new List&lt;CheckBoxSection&gt;(); string str = &#34;&#34;; foreach (Control c in pnlCheckBoxes.Controls) { if ((c is CheckBox) &amp;&amp; !((CheckBox)c).Checked) { str += c.UniqueID + &#34;, &#34;; } } items.Add(new CheckBoxSection { Section = ddlSection.SelectedValue, Check = str }); ViewState[&#34;items&#34;] = items; } else { List&lt;CheckBoxSection&gt; items = (List&lt;CheckBoxSection&gt;)ViewState[&#34;items&#34;]; string str = &#34;&#34;; foreach (Control c in pnlCheckBoxes.Controls) { if ((c is CheckBox) &amp;&amp; !((CheckBox)c).Checked) { str += c.UniqueID + &#34;, &#34;; } } bool isExist = true; for (int i = 0; i &lt; 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[&#34;items&#34;] = items; } } [Serializable] public class CheckBoxSection { public string Section { get; set; } public string Check { get; set; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">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(&#34;StuID&#34;, GetType(Integer)), New DataColumn(&#34;AdmissionNo&#34;, GetType(String)), New DataColumn(&#34;ReferenceNo&#34;, GetType(String)), New DataColumn(&#34;ParentID&#34;, GetType(String)), New DataColumn(&#34;AdmissionDate&#34;, GetType(String)), New DataColumn(&#34;SName&#34;, GetType(String)), New DataColumn(&#34;RollNo&#34;, GetType(String)), New DataColumn(&#34;DOB&#34;, GetType(String)), New DataColumn(&#34;SCNIC&#34;, GetType(String)), New DataColumn(&#34;FName&#34;, GetType(String)), New DataColumn(&#34;FCNIC&#34;, GetType(String)), New DataColumn(&#34;LeavingRemarks&#34;, GetType(String)), New DataColumn(&#34;Gender&#34;, GetType(String)), New DataColumn(&#34;Religion&#34;, GetType(String)), New DataColumn(&#34;BloodGroup&#34;, GetType(String)), New DataColumn(&#34;SPhone&#34;, GetType(String)), New DataColumn(&#34;FPhone&#34;, GetType(String)), New DataColumn(&#34;MPhone&#34;, GetType(String)), New DataColumn(&#34;GPhone&#34;, GetType(String)), New DataColumn(&#34;FJob&#34;, GetType(String)), New DataColumn(&#34;MName&#34;, GetType(String)), New DataColumn(&#34;MCNIC&#34;, GetType(String)), New DataColumn(&#34;MJob&#34;, GetType(String)), New DataColumn(&#34;Address&#34;, GetType(String)), New DataColumn(&#34;GuardianName&#34;, GetType(String)), New DataColumn(&#34;GCNIC&#34;, GetType(String)), New DataColumn(&#34;GAddress&#34;, GetType(String)), New DataColumn(&#34;Qualification&#34;, GetType(String)), New DataColumn(&#34;Income&#34;, GetType(String)), New DataColumn(&#34;Email&#34;, GetType(String)), New DataColumn(&#34;Active_Status&#34;, 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(&#34;items&#34;) IsNot Nothing Then Dim items As List(Of CheckBoxSection) = CType(ViewState(&#34;items&#34;), 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(&#34;,&#34;c).Length - 1 If Not String.IsNullOrEmpty(checkboxes.Split(&#34;,&#34;c)(j).Trim()) Then Dim checkboxId As String = checkboxes.Split(&#34;,&#34;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(&#34;items&#34;) Is Nothing Then Dim items As List(Of CheckBoxSection) = New List(Of CheckBoxSection)() Dim str As String = &#34;&#34; For Each c As Control In pnlCheckBoxes.Controls If (TypeOf c Is CheckBox) AndAlso Not (CType(c, CheckBox)).Checked Then str += c.UniqueID &amp; &#34;, &#34; End If Next items.Add(New CheckBoxSection With { .Section = ddlSection.SelectedValue, .Check = str }) ViewState(&#34;items&#34;) = items Else Dim items As List(Of CheckBoxSection) = CType(ViewState(&#34;items&#34;), List(Of CheckBoxSection)) Dim str As String = &#34;&#34; For Each c As Control In pnlCheckBoxes.Controls If (TypeOf c Is CheckBox) AndAlso Not (CType(c, CheckBox)).Checked Then str += c.UniqueID &amp; &#34;, &#34; 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(&#34;items&#34;) = items End If End Sub &lt;Serializable()&gt; Public Class CheckBoxSection Public Property Section As String Public Property Check As String End Class</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/eRtwYGb.gif" alt="" width="393" height="245" /></span></strong></p>https://www.aspforums.net:443/Threads/941694/Preserving-State-of-show-hide-ASPNet-GridView-Columns-using-C-and-VBNet/https://www.aspforums.net:443/Threads/941694/Preserving-State-of-show-hide-ASPNet-GridView-Columns-using-C-and-VBNet/Wed, 05 Dec 2018 06:58:47 GMT