运行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>
答案 0 :(得分:0)
除了从网格中导出数据之外,您还可以构建用于绑定网格的数据源的导出吗?
否则,如果您使用的是带有控件的模板化列,则必须从单元格中获取控件,然后从控件中检索值,如下所示:
(TextBox)GridView4.Rows[x].FindControl("TextBox1").Text;