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?
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.