Export To Excel

  • Thread starter Thread starter RN1
  • Start date Start date
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
 
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
 
Back
Top