0

我的程序有问题。当我这样做时:

GridView gvw = this.ProduttoriList1.FindControl("GridViewList") as GridView;
SqlDataSource dataSource = this.ProduttoriList1.FindControl(gvw.DataSourceID) as SqlDataSource;

FileInfo tempXlsFile = new FileInfo(HttpRuntime.CodegenDir + "\\" + Guid.NewGuid() + ".xlsx");
string filename = string.Format("Produttori_{0}.xlsx", Guid.NewGuid().ToString());
        try
        {
            gvw.AllowPaging = false;
            gvw.DataSourceID = string.Empty;
            gvw.DataSource = dataSource;
            gvw.DataBind();
            //...some export commands with EPPlus...
        }
        catch (Exception exception)
        {
            throw exception;
        }
        finally
        {
            System.IO.File.Delete(tempXlsFile.ToString());
        }

我要读取 HeaderRow,我无法读取具有 SortExpression 的列(返回“”值)。

<asp:Panel ID="Panel2" runat="server" Style="padding-top: 10px">
<asp:GridView ID="GridViewList" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSourceList"
    Width="100%" AllowPaging="True" AllowSorting="True" SkinID="GridViewDefault"
    DataKeyNames="id">
    <Columns>
        <asp:TemplateField HeaderStyle-Width="30px" HeaderText="N°">
            <ItemTemplate>
                <%# Container.DataItemIndex + 1 %>.
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="DescrLongITA" HeaderText="Produttore" SortExpression="DescrLongITA" />
        <asp:TemplateField HeaderText="Modifica">
            <ItemTemplate>
                <asp:ImageButton ID="ImageButtonEdit" runat="server" ImageUrl="~/Images/Icons/modifica.gif"
                    CommandArgument='<%# Eval("ID") %>' OnClick="ImageButtonEdit_Click" />
            </ItemTemplate>
            <HeaderStyle Width="60px" />
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Elimina">
            <ItemTemplate>
                <asp:ImageButton ID="ImageButtonDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/Icons/elimina.gif"
                    OnClientClick="return confirm(&quot;Continuare la cancellazione?&quot;)" CommandArgument='<%# Eval("ID") %>'
                    OnClick="ImageButtonDelete_Click" />
            </ItemTemplate>
            <HeaderStyle Width="60px" />
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
    </Columns>
    <EmptyDataTemplate>
        <div style="color: #FF0000; padding: 30px">
            <asp:Label ID="LabelEmpty1" runat="server" Text="Nessun dato trovato"></asp:Label>
        </div>
    </EmptyDataTemplate>
</asp:GridView>
</asp:Panel>

在这种情况下,我无法读取 HeaderText = "Produttore" 的 BoundField。

我能怎么做?

4

1 回答 1

1

我找到了解决方案,希望对您有所帮助。

当我执行导出程序的初步步骤并执行此操作时..

string[] columns = new string[gv.HeaderRow.Cells.Count];
for (int i = 0; i < columns.Length; i++)
{
   columns[i] = gv.HeaderRow.Cells[i].Text;
}
Export(tmpFile, gv, columns);

我已经换了这个

columns[i] = gv.HeaderRow.Cells[i].Text;

有了这个

var headertext = gv.Columns[i].HeaderText;
columns[i] = new DataColumn(headertext).ToString();
于 2018-05-23T13:56:20.497 回答