user5382234
user5382234

Reputation: 63

Hide Column In Grid

This is my html mark-up and adding the visibility to false in the div tag hides the actual data itself, but just leaves a blank column. I tried to access the div tag (yes I added the runat="server" tag to the html) and attempted to access it like so hideme.Visible = true; which threw a compile error of

Does not exist in the current context.

What should I alter/modify to ensure that this column is completely hidden from my grid?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="true"
        onrowdatabound="GridView1_RowDataBound"  onrowcreated="GridView1_RowCreated">
    <Columns>
      <asp:BoundField DataField="abcd" HeaderText="abcd" />
      <asp:BoundField DataField="def" HeaderText="def" />
      <asp:BoundField DataField="hij" HeaderText="hij" />
      <asp:BoundField DataField="xyz" HeaderText="xyz" />
      <asp:BoundField DataField="eee" HeaderText="eee" />
      <asp:BoundField DataField="era" HeaderText="era" />
      <asp:BoundField DataField="nai" HeaderText="nai" />
      <asp:BoundField DataField="fac" HeaderText="fac" />
      <asp:TemplateField>
            <ItemTemplate>
                <div runat="server" style="visibility:hidden" id="hideme">
                    <asp:Label ID="lbllunch" runat="server" Text='<%# Eval("hij") %>' />
                    <asp:Label ID="lbllunchoverage" runat="server" Text='<%# Eval("xyz") %>' />
                    <asp:Label ID="lbleee" runat="server" Text='<%# Eval("eee") %>' />
                    <asp:Label ID="lblera" runat="server" Text='<%# Eval("era") %>' />
                    <asp:Label ID="lblnai" runat="server" Text='<%# Eval("nai") %>' />
                    <asp:Label ID="lblfac" runat="server" Text='<%# Eval("fac") %>' />
                </div>
            </ItemTemplate>
      </asp:TemplateField>
    </Columns>
</asp:GridView>     

EDIT I added the .Visible command to my page load event (where I always hide anything on my page) like so:

protected void Page_Load(object sender, EventArgs e)
{
    hideme.Visible = false;
    /More here
}

Upvotes: 2

Views: 945

Answers (3)

arun G
arun G

Reputation: 224

If you want to hide template field column why you are adding it to markup. Yes if you keep style="visibility:hidden" it will show blank column only because item template still exists as column.

My suggestion for you is if dont need column dnt add markup.

Upvotes: 0

Developer
Developer

Reputation: 129

You can hide the column in DataBound event of gridview.

protected void GridView_DataBound(object sender, GridViewRowEventArgs e)
{    
    GridView.Columns[8].Visible = false;       

}

Upvotes: 0

ekad
ekad

Reputation: 14624

Since hideme is inside the GridView TemplateField, you can't access it in Page_Load method, however you can access it in GridView1_RowDataBound method as below

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // find the hideme div
        HtmlGenericControl div = (HtmlGenericControl)e.Row.FindControl("hideme");

        // set the visible property
        div.Visible = false;
    }
}

Upvotes: 1

Related Questions