Export To Excel

R

RN1

I want to export the data in a DataGrid to Excel. This is how I am
doing it (note that there are no LinkButtons in the DataGrid apart
from the Edit LinkButton which comes from the EditCommandColumn):

-------------------------
Sub Button_ExportToExcel(ByVal obj As Object, ByVal ea As EventArgs)
Dim stringWrite As System.IO.StringWriter
Dim htmlWrite As System.Web.UI.HtmlTextWriter

Response.Clear()
Response.AddHeader("content-disposition",
"attachment;filename=DG.xls")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.xls"
stringWrite = New System.IO.StringWriter()
htmlWrite = New HtmlTextWriter(stringWrite)
dgUsers.Columns.Remove(dgUsers.Columns.Item(5))
dgUsers.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
End Sub
-------------------------

The presence of the EditCommandColumn (which is the 6th column) with
the Edit LinkButton, I presume, generates the following error

------------------------
Control 'dgUsers_ctl02_ctl00' of type 'DataGridLinkButton' must be
placed inside a form tag with runat=server
------------------------

which is why I added the line
dgUsers.Columns.Remove(dgUsers.Columns.Item(5)) in the above code so
that the Edit link doesn't get exported to Excel but the above error
still gets generated.

How do I overcome this error?

Please note that if I get rid of the EditCommandColumn, then the
DataGrid gets exported to Excel successfully.

Thanks,

Ron
 
G

Guest

I want to export the data in a DataGrid to Excel. This is how I am
doing it (note that there are no LinkButtons in the DataGrid apart
from the Edit LinkButton which comes from the EditCommandColumn):

-------------------------
Sub Button_ExportToExcel(ByVal obj As Object, ByVal ea As EventArgs)
    Dim stringWrite As System.IO.StringWriter
    Dim htmlWrite As System.Web.UI.HtmlTextWriter

    Response.Clear()
    Response.AddHeader("content-disposition",
"attachment;filename=DG.xls")
    Response.Charset = ""
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.ContentType = "application/vnd.xls"
    stringWrite = New System.IO.StringWriter()
    htmlWrite = New HtmlTextWriter(stringWrite)
    dgUsers.Columns.Remove(dgUsers.Columns.Item(5))
    dgUsers.RenderControl(htmlWrite)
    Response.Write(stringWrite.ToString())
    Response.End()
End Sub
-------------------------

The presence of the EditCommandColumn (which is the 6th column) with
the Edit LinkButton, I presume, generates the following error

------------------------
Control 'dgUsers_ctl02_ctl00' of type 'DataGridLinkButton' must be
placed inside a form tag with runat=server
------------------------

which is why I added the line
dgUsers.Columns.Remove(dgUsers.Columns.Item(5)) in the above code so
that the Edit link doesn't get exported to Excel but the above error
still gets generated.

How do I overcome this error?

Please note that if I get rid of the EditCommandColumn, then the
DataGrid gets exported to Excel successfully.

Thanks,

Ron

http://www.c-sharpcorner.com/Upload...2005041447AM/ExportASPNetDataGridToExcel.aspx

Look at the "And just one more detail" chapter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,038
Messages
2,570,372
Members
47,018
Latest member
IrisN51866

Latest Threads

Top