导出到excel文件时隐藏子gridview列

时间:2016-07-21 14:02:28

标签: c# asp.net gridview export-to-excel

使用可见选项将grid-view导出到excel文件时,我可以隐藏父网格视图中的列。

但我也需要在子网格视图中隐藏一些列。

我如何实现这一目标?

导出代码

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代码

<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>

提前致谢。

1 个答案:

答案 0 :(得分:1)

通常,您可以使用gridView.FindControl("id-of-control")访问GridView中的控件。

在这种情况下,您可能会这样做。

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

这就是你如何访问内部GridView,并将其设置为visible = false。如果要访问内部GridView中的特定列或控件,只需在gridItem.FindControl("gvItemdt") GridView上调用相同的.FindControl()函数。或者通过列和行访问项目,但是你想要这样做。