0

我有一个要导出到 .csv 文件的 gridview。我将 gridview 绑定到数据表并使用 aspx 文件中的 and 标记。当我将 Gridview 设置为自动生成字段时,它会毫无问题地创建 csv 文件。但是,由于我需要编辑功能,我必须删除自动生成字段并使用 and 和我的代码,它只在 .csv 文件中创建列标题。代码:

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();

网格视图的 aspx 文件:

<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>

任何想法如何让它在gridview中实际写入行?

4

1 回答 1

0

我找到了一个解决方案......对于任何对此感兴趣的人来说都是工作代码:

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();
于 2012-08-31T13:46:49.663 回答