0

When I run my ExportTextFile method The data from my middle Credit File column is missing. I am thinking this is because it is an itemTemplate field but I need it to stay that way so I can hide a portion on that column when the gridview is displayed. So when I open the exported text file It only shows the column headers and the first column AppID and third column CreationDate. Is there a way to fix this?

relevant code

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>

1 Answers1

0

Instead of exporting the data from the grid, can you build the export off of the data source that you used to bind the grid with instead?

Otherwise, if you are using a templated column with control in it, you have to get the control from the cell and then retrieve the value from the control, something like this:

(TextBox)GridView4.Rows[x].FindControl("TextBox1").Text;
Pete -S-
  • 542
  • 5
  • 13
  • How would I go about doing that? – SamTheMan29 Jul 06 '20 at 20:14
  • @SamTheMan29: For the first part, check this out: https://stackoverflow.com/questions/785799/how-can-i-export-a-gridview-datasource-to-a-datatable-or-dataset and for the second part. In your loop you would have add the code to retrieve the value of the templated column control and write it out to your stream. – Pete -S- Jul 06 '20 at 20:24
  • Another options: https://forums.asp.net/t/2017258.aspx?Generate+txt+file+from+sql+code, it kind of looks like you started out using the answer from https://social.msdn.microsoft.com/Forums/windows/en-US/9d57a285-a711-4848-9cd1-e1e6daed3f98/how-to-export-data-from-gridview-to-plain-text-file – Pete -S- Jul 06 '20 at 20:37