Search code examples
c#asp.netgridviewexport-to-excel

Hide child gridview columns when exporting to excel file


I am able to hide columns in parent grid-view when exporting grid-view to excel file using visible option.

But i need to hide some columns in child grid-view as well.

How can i achieve that ?

Export Code

protected void btnexcel_Click(object sender, EventArgs e)
{
    try
    {
        gridItem.DataSource = i.GetItems();
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition",
         "attachment;filename=Items.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        gridItem.GridLines = GridLines.Both;
        gridItem.DataBind();
        gridItem.Columns[0].Visible = false;
        gridItem.RenderControl(hw);
        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
    catch (Exception ex)
    {
        Response.Write("Error: " + ex.Message);
    }
}

Aspx Code

<asp:GridView ID="gridItem" GridLines="None" OnRowDataBound="gridItem_RowDataBound" OnRowCommand="gridItem_RowCommand" DataKeyNames="ItemID" runat="server" CssClass="table" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="Preview">
            <ItemTemplate>
                <asp:Image ID="imgPlus" runat="server" AlternateText="" ImageUrl="img/plus.png" Style="cursor: pointer" />
                <asp:Panel ID="pnItemdt runat="server" Style="display: none">
                    <asp:GridView ID="gvItemdt" CssClass="table table-bordered" runat="server" OnRowCommand="gvItemdt_RowCommand" AutoGenerateColumns="false">
                        <Columns>
                            <asp:TemplateField HeaderText="Edit">
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" OnClientClick="gotop()" runat="server" CssClass="btn btn-primary btn-xs" CausesValidation="False" CommandName="Editob" Text=""><i class="glyphicon glyphicon-pencil"></i></asp:LinkButton>
                                </ItemTemplate>
                                <ControlStyle CssClass="btn btn-primary" />
                            </asp:TemplateField>
                            <asp:BoundField DataField="Name" HeaderText="Store Name" />
                        </Columns>
                        <EmptyDataTemplate>
                            "No records found"
                        </EmptyDataTemplate>
                    </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="ItemCode" HeaderText="Item Code" SortExpression="ItemCode" />
        <asp:BoundField DataField="ItemName" HeaderText="Item Name" SortExpression="ItemName" />

    </Columns>

</asp:GridView>

Thanks in advance.


Solution

  • Typically you can access controls that are within a GridView using gridView.FindControl("id-of-control").

    In this scenario, you might do something like this.

    gridItem.FindControl("gvItemdt") //this gets you the GridView control
    //you can use DirectCast to call a GridView function on the object
    DirectCast(gridItem.FindControl("gvItemdt"), GridView).Visible=False
    

    Thats how you'd access the inner GridView, and set it to visible=false. If you want to access a particular column or control within the inner GridView, just call the same .FindControl() function on the gridItem.FindControl("gvItemdt") GridView. Or access the items via columns and rows, however you want to do it.