将网格视图导出到 txt 文档时缺少列



当我运行我的导出文本文件方法时,中间信用文件列中的数据丢失。我认为这是因为它是一个 itemTemplate 字段,但我需要它保持这种状态,以便在显示网格视图时隐藏该列的一部分。因此,当我打开导出的文本文件时,它仅显示列标题以及第一列AppID和第三列CreationDate。有没有办法解决这个问题?

相关代码

protected void ExportTextFile (object sender, EventArgs e)
{
String strDestinationFile;
strDestinationFile = "C:\CreditFile.txt";
TextWriter tw = new StreamWriter(strDestinationFile);
//writing the header
for (int x = 0; x < GridView4.Columns.Count; x++)
{
tw.Write(GridView4.Columns[x].HeaderText);
if (x != GridView4.Columns.Count - 1)
{
tw.Write(", ");
}
}
tw.WriteLine();
//writing the data
for (int x = 0; x < GridView4.Rows.Count - 1; x++)
{
for (int y = 0; y < GridView4.Columns.Count; y++)
{
tw.Write($"{GridView4.Rows[x].Cells[y].Text.ToString()}");
if (y != GridView4.Columns.Count - 1)
{
tw.Write(", ");
}
}
tw.WriteLine();
}
tw.Close();
}
<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" SortExpression="CreditFile">
<ItemTemplate>

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CreditFile") %>'></asp:TextBox>

</ItemTemplate>
<ItemTemplate>
<div style="width: 100px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;">
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CreditFile") %>'></asp:Label>
</div>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Wrap="True" />
</asp:TemplateField>
<asp:BoundField DataField="CreationDate" DataFormatString="{0:d}" HeaderText="CreationDate" 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>

是否可以从用于绑定网格的数据源构建导出,而不是从网格导出数据?

否则,如果您使用的是包含控件的模板化列,则必须从单元格中获取控件,然后从控件中检索值,如下所示:

(TextBox)GridView4.Rows[x].FindControl("TextBox1").Text;

最新更新