Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Refering to your article on exporting the gridview to Word/Excel/PDF/CSV, I modified to code to populate the GV dynamically coz they have different data and different columns as per the selection. But after the data population, when I try to export the data it does not export any data instead for eg. in Word it just export.
Any help would be appreciated.

The code is pasted below.

**** The below is the code for populating GV ********
VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dt As DataTable = GetData()
        Dim dcol As DataColumn
        Dim bfields As BoundField
 
        GridView1.Columns.Clear()
        For Each dcol In dt.Columns
            bfields = New BoundField
            bfields.DataField = dcol.ColumnName
            bfields.HeaderText = dcol.ColumnName
            GridView1.Columns.Add(bfields)
        Next
 
        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub

**** Below Code is from your samples for exporting GV data ******
VB
Protected Sub btnExportWord_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-word "
        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        GridView1.AllowPaging = False
        GridView1.DataBind()
        GridView1.RenderControl(hw)
        Response.Output.Write(sw.ToString())
        Response.Flush()
        Response.End()
    End Sub
Posted
v2
Comments
[no name] 8-Nov-12 4:04am    
according to which article, you use which tool?

1 solution

Public Shared Sub ExportDataSetToExcel(ds As DataSet, filename As String)
Dim response As HttpResponse = HttpContext.Current.Response

'Clean response object
response.Clear()
response.Charset = ""

'Set response header
response.ContentType = "application/vnd.ms-excel"
response.AddHeader("Content-Disposition", "attachment;filename=""" & filename & """")

'Create StringWriter and use to create CSV
Using sw As New StringWriter()
Using htw As New HtmlTextWriter(sw)
'Instantiate DataGrid
Dim dg As New DataGrid()
dg.DataSource = dt
dg.DataBind()
dg.RenderControl(htw)
response.Write(sw.ToString())
response.[End]()
End Using
End Using
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900