In this article I will explain with an example, how to dynamically show hide TemplateField (ItemTemplate) column in ASP.Net GridView using C# and VB.Net.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net GridView and a CheckBox. The GridView contains two BoundField columns and one TemplateField column. The CheckBox has been assigned an OnCheckChanged event handler and AutoPostBack property is set to True.
<asp:CheckBox ID="chkCountry" Text="Show Hide Country" runat="server" OnCheckedChanged="OnCheckedChanged"
    AutoPostBack="true" Checked="true" />
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
            <ItemTemplate>
                <asp:Label Text='<%# Eval("Country") %>' runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the ASP.Net GridView control
The GridView is populated with a dynamic DataTable with some dummy data inside the Page Load event.
Note: You can learn more about this dynamic DataTable in my article Create DataTable dynamically and bind to GridView in ASP.Net.
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
        dt.Rows.Add(1, "John Hammond", "United States")
        dt.Rows.Add(2, "Mudassar Khan", "India")
        dt.Rows.Add(3, "Suzanne Mathews", "France")
        dt.Rows.Add(4, "Robert Schidner", "Russia")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
 
Dynamically Show Hide TemplateField column in ASP.Net GridView
Inside the OnCheckChanged event handler, the third column i.e. Country column of the GridView is made visible or hidden based on the whether the CheckBox is checked or unchecked respectively.
C#
protected void OnCheckedChanged(object sender, EventArgs e)
{
    GridView1.Columns[2].Visible = (sender as CheckBox).Checked;
}
 
VB.Net
Protected Sub OnCheckedChanged(sender As Object, e As EventArgs)
    GridView1.Columns(2).Visible = TryCast(sender, CheckBox).Checked
End Sub
 
 
Screenshot
Dynamically Show Hide TemplateField column in ASP.Net GridView using C# and VB.Net
 
 
Demo
 
 
Downloads