导出具有TemplateField的gridview数据

问题描述

将我的gridview导出到文本文件时,缺少TemplateField列中的数据。我必须在该列上添加省略号,因为否则列数据太长,并且会占用整个屏幕,因此CreditFile列需要保留TemplateField。

有问题的网格视图

<asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="AppID,ServiceRequestID,ServiceRequestCreditFileID" DataSourceID="sqlDataSource2" GridLines="Horizontal">
    <AlternatingRowStyle BackColor="#F7F7F7" />
    <Columns>
        <asp:BoundField datafield="AppID" HeaderText="AppID" ReadOnly="True" SortExpression="AppID">
        <ItemStyle HorizontalAlign="Left" />
        </asp:BoundField>
        <asp:TemplateField HeaderText="Credit File">
            <ItemTemplate>
                <div style="width:60px; overflow:hidden; text-overflow: ellipsis; white-space:Nowrap;">
                    <asp:Label ID="lblCreditFile" runat="server" Text='<%#Eval("CreditFile") %>'  ToolTip='<%#Eval("CreditFile") %>'></asp:Label>
                </div>
             </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField datafield="CreationDate" DataFormatString="{0:d}" HeaderText="Creation Date" SortExpression="CreationDate">
        <ItemStyle HorizontalAlign="Left" />
        </asp:BoundField>
    </Columns>
    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
    <SortedAscendingCellStyle BackColor="#F4F4FD" />
    <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
    <SortedDescendingCellStyle BackColor="#D8D8F0" />
    <SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>

导出方法

protected void ExportTextFile(object sender,EventArgs e)
{
    //the text file
    string sFileName = "Credit File.txt";
    sFileName = sFileName.Replace("/","");

    //send output to the client maching using response object
    Response.ClearContent();
    Response.Buffer = true;
    response.addheader("content-disposition","attachment; filename=" + sFileName);
    Response.ContentType = "application/text";
    EnableViewState = false;

    StringBuilder objSB = new StringBuilder();
    int iCol = 0; //counter for columns
    int iRow = 0; //counter for rows



    for (iCol = 0; iCol < GridView4.Columns.Count; iCoL++)
    {
        objSB.Append(GridView4.Columns[iCol].HeaderText + ',');
    }

    objSB.Append(Environment.NewLine);

    for (iRow = 0; iRow < GridView4.Rows.Count; iRow++)
    {
        for (iCol = 0; iCol < GridView4.Columns.Count; iCoL++)
        {
            objSB.Append(GridView4.Rows[iRow].Cells[iCol].Text + ',');
        }

        objSB.Append(Environment.NewLine);
    }

    Response.Write(objSB.ToString());
    Response.End();
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)