Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8470615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:34:08+00:00 2026-06-10T16:34:08+00:00

I have a gridview that I want to export to a .csv file. I

  • 0

I have a gridview that I want to export to a .csv file. I have the gridview bound to a datatable and use the and tags in the aspx file. When I set the Gridview to autogenerate the fields, it creates the csv file without any problems at all. However, since I needed the editing capabilities, I have to remove the autogenerate fields and use the and and with my code, it only creates the Column Headings in the .csv file.
CODE:

StreamWriter sw = new StreamWriter(@"C:\Web_Order\Order_W" + custordernum.ToString() + ".csv");
// Write columns 
sw.Write(griditems.Columns[0].HeaderText);
for (int i = 1; i < griditems.Columns.Count; i++)
    sw.Write("," + griditems.Columns[i].HeaderText);
sw.Write("\n");

// Write values 
for (int x = 0; x < griditems.Rows.Count; x++)
{
    sw.Write(griditems.Rows[x].Cells[0].Text);
    for (int i = 1; i < griditems.Rows[x].Cells.Count; i++)
        sw.Write("," + griditems.Rows[x].Cells[i].Text);
    sw.Write("\n");
 }
 sw.Close(); 
 sw.Dispose();

The aspx file for the gridview:

<asp:GridView ID="griditems" runat="server" 
        onrowdeleting="griditems_RowDeleting" onrowediting="griditems_RowEditing" onrowupdating="griditems_RowUpdating"
                  AllowPaging="True" 
        onpageindexchanging="griditems_PageIndexChanging" Onrowcancelingedit="griditems_RowCancelingEdit" 
                  Caption="Order Details" AutoGenerateDeleteButton="True" 
        AutoGenerateEditButton="True" 
        AutoGenerateColumns="False" >            
        <EditRowStyle BackColor="#FF9900" BorderStyle="Double"/> 
        <HeaderStyle Font-Bold="True" Font-Italic="False" />
        <RowStyle HorizontalAlign="Center"/>
        <Columns> 
          <asp:TemplateField HeaderText="Part Number">
            <ItemTemplate>
              <asp:Label ID = "partlbl" runat="server" Text='<%#Eval("Part Number") %>'></asp:Label> 
            </ItemTemplate>
            <EditItemTemplate>
               <asp:TextBox ID="partedit" runat="server" Text='<%# Eval("Part Number")%>'  ></asp:TextBox>
            </EditItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
              <asp:Label ID = "qtylbl" runat="server" Text='<%#Eval("Quantity") %>'></asp:Label> 
            </ItemTemplate>
            <EditItemTemplate>
               <asp:TextBox ID="qtyedit" runat="server" Text='<%# Eval("Quantity")%>'  ></asp:TextBox>
            </EditItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Ship-To">
            <ItemTemplate>
              <asp:Label ID = "shiptolbl" runat="server" Text='<%#Eval("Ship-To") %>'></asp:Label> 
            </ItemTemplate>
            <EditItemTemplate>
               <asp:TextBox ID="shiptoedit" runat="server" Text='<%# Eval("Ship-To")%>'  ></asp:TextBox>
            </EditItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Requested Date">
            <ItemTemplate>
              <asp:Label ID = "reqdatelbl" runat="server" Text='<%#Eval("Requested Date") %>'></asp:Label> 
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Calendar ID="reqdatecaledit" runat="server" BackColor="White" BorderColor="#3366CC" BorderWidth="1px" CellPadding="1" 
                              DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="#003399" Height="200px" Width="220px" 
                              ondayrender="reqdatecal_DayRender" ShowGridLines="True">
                              <DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
                              <DayStyle BackColor="White" />
                              <NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
                              <OtherMonthDayStyle ForeColor="#999999" />
                              <SelectedDayStyle BackColor="#FF9900" Font-Bold="True" ForeColor="#CCFF99" />
                              <SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
                              <TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px" Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF" 
                                          Height="25px" />
                              <TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
                              <WeekendDayStyle BackColor="#CCCCFF" /></asp:Calendar>
            </EditItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Shipping Method">  
            <ItemTemplate><asp:Label ID="shipmthdlbl" runat="server" Text='<%#Eval("Shipping Method") %>'></asp:Label>   
            </ItemTemplate>              
              <EditItemTemplate>            
                <asp:DropDownList ID="shipmthdedit" runat="server">                
                  <asp:ListItem>FedEx Ground (1-5 Business Days)</asp:ListItem>
                  <asp:ListItem>FedEx 3 Business Days</asp:ListItem>
                  <asp:ListItem>FedEx 2 Business Days</asp:ListItem>
                  <asp:ListItem>FedEx Overnight</asp:ListItem>   
                </asp:DropDownList>    
              </EditItemTemplate>  
            </asp:TemplateField>    
        </Columns>    
    </asp:GridView>

Any ideas how to get it to actually write the rows in the gridview?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:34:10+00:00Added an answer on June 10, 2026 at 4:34 pm

    I found a solution…for anyone interested here is the working code:

    DataTable dt = (DataTable)Session["table"]; 
                    int iColCount = dt.Columns.Count; 
                    for (int i = 0; i < iColCount; i++) 
                    { 
                        sw.Write(dt.Columns[i]); 
                        if (i < iColCount - 1) 
                        { 
                            sw.Write(","); 
                        } 
                    } 
                    sw.Write(sw.NewLine);
                    // Now write all the rows.
                    foreach (DataRow dr in dt.Rows)        
                    {            
                        for (int i = 0; i < iColCount; i++) 
                        {               
                            if (!Convert.IsDBNull(dr[i]))         
                            {      
                                sw.Write(dr[i].ToString());   
                            }            
                            if (i < iColCount - 1)    
                            {                 
                                sw.Write(",");    
                            }           
                        }        
                        sw.Write(sw.NewLine);   
                    }       
                    sw.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a GridView that is bound to an Entity Framework object. I want
I have a gridview I bound a DataTable with that Gridview Its dynamic so
I have a gridview that displays items details, I added two template fields one
I have a gridview that is bound to a datasource on a Windows Form
I have a gridview that is bound to a datasource (Windows Forms, VB.NET). One
I have a gridview that I am exporting to an excel file. When I
Basically I have a webcontrol that contains a gridview with an export button. When
I have a gridview that is populated by a dataset and I want to
I have a link in a Gridview that I want opened in Windows Explorer
I have a gridview that takes in values from a sortedlist. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.