title: how to hide templatefield in gridview
body: <p>I am repeating this question because i am not able to find answer from anywhere.</p>
  
  <p>I Have a <code>GridView</code> in .aspx page. I want to hide columns based on aspx.cs <code>BindData()</code> method.</p>
  
  <p>I have tried using below code but not able to hide.  I am using Asp.net with C#.</p>
  
  <p>Below is my <code>GridView</code> with columns and I have also included the <code>Button</code> click code.</p>
  
  <p>If I <code>select "T-L"</code> which is in below <code>else-if Ladder</code> i am getting this <code>error</code></p>
  
  <p><strong>DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'tutorial'.</strong><br>
  I have marked a test case error in .aspx</p>
  
  <p>For temporary to make my program work i am using 4 gridview to bind 4 query,which is not feasible...How can i hide templatefield invisible based on conditions...
  plz help me..</p>
  
  <pre><code>&lt;GridView&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField DataField="id" HeaderText="Id" SortExpression="id" 
              Visible="false" /&gt;                     
          &lt;asp:TemplateField HeaderText="RollNo" &gt;
              &lt;ItemTemplate&gt;
                  &lt;%# Eval("st_rollno")%&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;    
                  &lt;asp:TextBox ID="tbsturollno" runat="Server"
                      Text='&lt;%# Eval("st_rollno") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;   
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Name"&gt;
              &lt;ItemTemplate&gt;
                  &lt;%# Eval("st_name")%&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;    
                  &lt;asp:TextBox ID="tbstuname" runat="Server"
                      Text='&lt;%# Eval("st_name") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;               
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Theory"&gt;
              &lt;ItemTemplate&gt;
                  &lt;%# Eval("theory")%&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt; 
                  &lt;asp:TextBox ID="tbtheory" runat="Server"
                      Text='&lt;%# Eval("theory") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;   
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Total" &gt;
              &lt;ItemTemplate&gt;
                  &lt;%# Eval("ttotal")%&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;   
                  &lt;asp:TextBox ID="tbtheorytotal" runat="Server"
                      Text='&lt;%# Eval("ttotal") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;   
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Lab" &gt;
              &lt;ItemTemplate&gt;
                  &lt;%# Eval("lab")%&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:TextBox ID="tblab" runat="Server"
                      Text='&lt;%# Eval("lab") %&gt;'&gt;
                  &lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;   
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Total" &gt;
              &lt;ItemTemplate&gt;
                  &lt;%# Eval("ltotal")%&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:TextBox ID="tblabtotal" runat="Server"
                      Text='&lt;%# Eval("ltotal") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;   
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Tutorial" &gt;
              &lt;ItemTemplate&gt;
     *Error is HERE             &lt;%# Eval("tutorial")%&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;   
                  &lt;asp:TextBox ID="tbtutorial" runat="Server"
                      Text='&lt;%# Eval("tutorial") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;   
          &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Total" &gt;
              &lt;ItemTemplate&gt;
                  &lt;%# Eval("tutotal")%&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;    
                  &lt;asp:TextBox ID="tbtutorialtotal" runat="Server"
                      Text='&lt;%# Eval("tutotal") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;   
          &lt;/asp:TemplateField&gt;        
      &lt;/Columns&gt;
  &lt;/GridView&gt;
  
  private void BindData()
  {
      DataTable dt = new DataTable();
      using (SqlConnection con = new SqlConnection(ConnectionString))
      {
          if (stype.Equals("L"))
          {
              query = "SELECT [id], [st_rollno], [st_name], [lab], [ltotal] FROM [Attendence_Subject_Wise] WHERE (([branch_name] = @branch_name) AND ([scode] = @scode) AND ([sem_no] = @sem_no) AND ([sess_no] = @sess_no)) ORDER BY [st_rollno]";
              GridView1.Columns[3].Visible = false;
              GridView1.Columns[4].Visible = false;
              GridView1.Columns[7].Visible = false;
              GridView1.Columns[8].Visible = false;
          }
          else if (stype.Equals("T"))
          {
              query = "SELECT [id], [st_rollno], [st_name], [theory], [ttotal] FROM [Attendence_Subject_Wise] WHERE (([branch_name] = @branch_name) AND ([scode] = @scode) AND ([sem_no] = @sem_no) AND ([sess_no] = @sess_no)) ORDER BY [st_rollno]";
              GridView1.Columns[5].Visible = false;
              GridView1.Columns[6].Visible = false;
              GridView1.Columns[7].Visible = false;
              GridView1.Columns[8].Visible = false;
          }
          else if (stype.Equals("T-L"))
          {
              GridView1.Columns[7].Visible = false;
              GridView1.Columns[8].Visible = false;    
              query = "SELECT [id], [st_rollno], [st_name], [theory], [ttotal], [lab], [ltotal] FROM [Attendence_Subject_Wise] WHERE (([branch_name] = @branch_name) AND ([scode] = @scode) AND ([sem_no] = @sem_no) AND ([sess_no] = @sess_no)) ORDER BY [st_rollno]";
          }
          else
          {    
              query = "SELECT [id], [st_rollno], [st_name],[theory], [ttotal], [lab], [ltotal], [tutorial], [tutotal] FROM [Attendence_Subject_Wise] WHERE (([branch_name] = @branch_name) AND ([scode] = @scode) AND ([sem_no] = @sem_no) AND ([sess_no] = @sess_no)) ORDER BY [st_rollno]";
          }
  
          com = new SqlCommand(query);
          com.Parameters.Add("@branch_name", dept);
          com.Parameters.Add("@scode", dpsubject.SelectedItem.Text.ToString());
          com.Parameters.Add("@sem_no", Int32.Parse(dpsemno.SelectedItem.Text.ToString()));
          com.Parameters.Add("@sess_no",Int32.Parse(dpsessional.SelectedItem.Text.ToString()));
  
          using (SqlDataAdapter sda = new SqlDataAdapter())
          {
              com.Connection = con;
              con.Open();
              sda.SelectCommand = com;
              sda.Fill(dt);
              GridView1.DataSource = dt;
              GridView1.DataBind();
              con.Close();
          }    
      }
  }
  
  protected void bsubmit_Click(object sender, EventArgs e)
  {
      this.BindData();
  }
  </code></pre>