TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Alfredo Guzman
NA
1
0
Export a GridView (No a DtaGrid) to Excel
Jul 18 2005 10:27 AM
Hi
I hope you could help me. I have been trying to export a Gridview to excel, but
there is an error that says "Control 'GridView1' of type 'GridView' must be
placed inside a form tag with runat=server. " I test the code with a DataGrid
and it works well, but I need to use it with a GridView, I´d like to know if
you could help, I have been searching for examples with this control, but I
could not find anyone.
Please help me is very urgent.
My code with out the function that I found to export a DataGrid to excel is the
following, I builded this code with the visual web developer 2005 beta:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd
">
<html xmlns="
http://www.w3.org/1999/xhtml
" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButtonList ID="RadioButtonList2" runat="server"
AutoPostBack="True" Style="z-index: 101;
left: 504px; position: absolute; top: 12px">
<asp:ListItem Selected="True">GMS</asp:ListItem>
<asp:ListItem>LADO</asp:ListItem>
<asp:ListItem>Las Fuentes</asp:ListItem>
</asp:RadioButtonList>
</div>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
Style="z-index: 103; left: 262px; position: absolute; top: 12px"
AutoPostBack="True">
<asp:ListItem Selected="True">9:15 AM</asp:ListItem>
<asp:ListItem>3:15 PM</asp:ListItem>
</asp:RadioButtonList>
<asp:RadioButtonList ID="RadioButtonList3" runat="server"
AutoPostBack="True" Style="z-index: 104;
left: 642px; position: absolute; top: 13px">
<asp:ListItem Selected="True">GMS</asp:ListItem>
<asp:ListItem>LADO</asp:ListItem>
<asp:ListItem>Las Fuentes</asp:ListItem>
</asp:RadioButtonList>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False"
CellPadding="4" DataSourceID="AccessDataSource1" ForeColor="#333333"
GridLines="None"
Style="z-index: 105; left: 44px; position: absolute; top: 122px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"
/>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="folioEnvio" HeaderText="folioEnvio"
SortExpression="folioEnvio" />
<asp:BoundField DataField="NombreRemitente"
HeaderText="NombreRemitente" SortExpression="NombreRemitente" />
<asp:BoundField DataField="SiteOrigen" HeaderText="SiteOrigen"
SortExpression="SiteOrigen" />
<asp:BoundField DataField="NombreDestinatario"
HeaderText="NombreDestinatario" SortExpression="NombreDestinatario" />
<asp:BoundField DataField="SiteDestino" HeaderText="SiteDestino"
SortExpression="SiteDestino" />
<asp:BoundField DataField="Descripcion" HeaderText="Descripcion"
SortExpression="Descripcion" />
<asp:BoundField DataField="FechaTramite"
HeaderText="FechaTramite" SortExpression="FechaTramite" />
<asp:BoundField DataField="StatusEnvio" HeaderText="StatusEnvio"
SortExpression="StatusEnvio" />
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"
/>
<EditRowStyle BackColor="#999999" />
<alternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="C:\mensajeria.mdb"
SelectCommand="SELECT Remitente.folioEnvio,
Remitente.NombreRemitente, Remitente.SiteOrigen,
Destinatario.NombreDestinatario, Destinatario.SiteDestino,
SolicitudEnvios.Descripcion, SolicitudEnvios.FechaTramite,
SolicitudEnvios.StatusEnvio FROM ((Remitente INNER JOIN Destinatario ON
Remitente.folioEnvio = Destinatario.folioEnvio) INNER JOIN SolicitudEnvios ON
Remitente.folioEnvio = SolicitudEnvios.FolioEnvio) WHERE (Remitente.SiteOrigen
= ?) AND (Destinatario.SiteDestino = ?) AND (SolicitudEnvios.FechaTramite <= ?)
AND (SolicitudEnvios.StatusEnvio = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="RadioButtonList2"
DefaultValue="GMS" Name="?" PropertyName="SelectedValue"
Type="String" />
<asp:ControlParameter ControlID="RadioButtonList3"
DefaultValue="LADO" Name="?" PropertyName="SelectedValue"
Type="String" />
<asp:ControlParameter ControlID="RadioButtonList1"
DefaultValue="9:15 AM" Name="?"
PropertyName="SelectedValue" Type="DateTime" />
<asp:Parameter DefaultValue="Procesado" Name="?" Type="String"
/>
</SelectParameters>
</asp:AccessDataSource>
<asp:Button ID="Button1" runat="server" Style="z-index: 107;
left: 440px; position: absolute; top: 528px" Text="Button"
OnClick="Button1_Click" />
</form>
</body>
</html>
The function that I found to export a DataGrid to Excel is the following, I
changed the object DataGrid to Gridview, but the error that I show you above,
makes that it doesnt work:
protected void Button1_Click(object sender, EventArgs e)
{
//export to excel
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(GridView1);
GridView1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
private void ClearControls(Control control)
{
for (int i = control.Controls.Count - 1; i >= 0; i--)
{
ClearControls(control.Controls[i]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text =
(string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else
if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text =
(string)control.GetType().GetProperty("Text").GetValue(control, null);
control.Parent.Controls.Remove(control);
}
}
return;
}
I really hope you can help me
Thaks for ALL
Reply
Answers (
0
)
Delete/Remove the Last Row of a HTML Table in asp.net page
ListBox : how to disable an item in a ListBox?