Reputation: 11
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("Continuare la cancellazione?")" 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
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