How to hide a TemplateField column in a GridView

117,629

Solution 1

protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
         e.Row.Cells[columnIndex].Visible = false;
}


If you don't prefer hard-coded index, the only workaround I can suggest is to provide a HeaderText for the GridViewColumn and then find the column using that HeaderText.

protected void UsersGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
    ((DataControlField)UsersGrid.Columns
            .Cast<DataControlField>()
            .Where(fld => fld.HeaderText == "Email")
            .SingleOrDefault()).Visible = false;
}

Solution 2

For Each dcfColumn As DataControlField In gvGridview.Columns
    If dcfColumn.HeaderText = "ColumnHeaderText" Then
        dcfColumn.Visible = false                    
    End If
Next

Solution 3

If appears to me that rows where Visible is set to false won't be accessible, that they are removed from the DOM rather than hidden, so I also used the Display: None approach. In my case, I wanted to have a hidden column that contained the key of the Row. To me, this declarative approach is a little cleaner than some of the other approaches that use code.

<style>
   .HiddenCol{display:none;}                
</style>


 <%--ROW ID--%>
      <asp:TemplateField HeaderText="Row ID">
       <HeaderStyle CssClass="HiddenCol" />
       <ItemTemplate>
       <asp:Label ID="lblROW_ID" runat="server" Text='<%# Bind("ROW_ID") %>'></asp:Label>
       </ItemTemplate>
       <ItemStyle HorizontalAlign="Right" CssClass="HiddenCol" />
       <EditItemTemplate>
       <asp:TextBox ID="txtROW_ID" runat="server" Text='<%# Bind("ROW_ID") %>'></asp:TextBox>
       </EditItemTemplate>
       <FooterStyle CssClass="HiddenCol" />
      </asp:TemplateField>

Solution 4

GridView1.Columns[columnIndex].Visible = false;

Solution 5

try this

.hiddencol
    {
        display:none;
    }
    .viscol
    {
        display:block;
    }

add following code on RowCreated Event of GridView

protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         e.Row.Cells[0].CssClass = "hiddencol";
     }
     else if (e.Row.RowType == DataControlRowType.Header)
     {
         e.Row.Cells[0].CssClass = "hiddencol";
     }
}
Share:
117,629
Homam
Author by

Homam

It's early to write about me but I'll do.

Updated on July 13, 2022

Comments

  • Homam
    Homam almost 2 years

    How can I hide a TemplateField column in a GridView?

    I tried the following:

    <asp:TemplateField ShowHeader="False" Visible='<%# MyBoolProperty %>' >
    <ItemTemplate>
        <asp:LinkButton ID="attachmentButton" runat="server" ... />
    </ItemTemplate>
    

    but it didn't work and gives the following error:

    Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.TemplateField does not have a DataBinding event.

    I tried also to hide it programmatically, but seems it's not possible to get a column by the name because there iss no name for TemplateField column.

  • Homam
    Homam about 13 years
    Thanks, but I wonder how can I get its index, I don't prefer the hard-coded indexes.
  • Nathan Koop
    Nathan Koop almost 12 years
    this is fine if all you care about is the visibility on the screen, however if you are showing/hiding private information (IE stackoverflow real name) then you cannot just hide it on the client, it can't be sent to the client at all
  • Gk_999
    Gk_999 almost 10 years
    @naveen e.Row.Cells[columnIndex].Visible = false; hides column data perfectly.... What if i also want to hide corresponding Header Template??
  • naveen
    naveen almost 10 years
    @Gk_999: It hides the header template too
  • Caesar
    Caesar over 9 years
    @naveen your solution with DataControlField works also after databind the only advise is to put the title of column in this way: <asp:TemplateField HeaderText ="Email">
  • Fandango68
    Fandango68 almost 9 years
    Problem with that is that you cannot place a <div> outside of a template field.
  • frenchone
    frenchone almost 9 years
    Seemed quite obvious to me that the div was inside the templatefield. Anyway I edited to make it more obvious than obvious
  • A-Sharabiani
    A-Sharabiani over 5 years
    What if the HeaderText is localized?
  • naveen
    naveen over 5 years
    @A-Sharabiani: been a while. Will check when I get time
  • Tomas Beblar
    Tomas Beblar over 4 years
    Doing it on OnDataBound event will only run it once instead of once per row. This may be prefer if the function is heavy.