从Gridview itemtemplate字段导出CSV

时间:2012-08-31 13:28:40

标签: c# asp.net gridview

我有一个gridview,我想导出到.csv文件。我将gridview绑定到数据表并使用aspx文件中的和标记。当我将Gridview设置为自动生成字段时,它会创建csv文件而不会出现任何问题。但是,由于我需要编辑功能,我必须删除自动生成字段并使用和我的代码,它只在.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();

gridview的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中实际写入行?

1 个答案:

答案 0 :(得分: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();