Michele Abate
Michele Abate

Reputation: 11

GridView with SortExpression in column - I can't read the HeaderRow to make an Excel file with EPPlus - C Sharp C#

I' ve got an issue with my program. When i do this:

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());
        }

and i'm going to read the HeaderRow, i can't read the column that has a SortExpression (return a "" value).

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

In this case, i can't read the BoundField with HeaderText = "Produttore".

How can i do?

Upvotes: 0

Views: 181

Answers (1)

Michele Abate
Michele Abate

Reputation: 11

I found the solution, i hope to help you.

When I perform the preliminary steps of the export procedure and do this..

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

i've replaced this one

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

with this one

var headertext = gv.Columns[i].HeaderText;
columns[i] = new DataColumn(headertext).ToString();

Upvotes: 1

Related Questions