【发布时间】:2020-10-27 00:30:51
【问题描述】:
当我运行我的 ExportTextFile 方法时,我的中间信用文件列中的数据丢失了。我认为这是因为它是一个 itemTemplate 字段,但我需要它保持这种状态,以便在显示 gridview 时隐藏该列上的一部分。所以当我打开导出的文本文件时,它只显示列标题和第一列 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>
【问题讨论】: