Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Databind export problem

Status
Not open for further replies.

stephenk1973

Technical User
Jun 11, 2003
246
GB
I have a stored procedure which i use to populate a gridview. Some of the gridview columns have visible set false but on export i make them visible by looping through all the columns. My export button is on the master page, code, ....

Dim gd As New GridView
gd = Me.Page.Controls(0).FindControl("MainFormData").FindControl("Maincontent").FindControl("GridView1")


Dim i As Integer

For i = 0 To (gd.Columns.Count() - 1)
gd.Columns(i).Visible = True
Next i

gd.AllowPaging = False
gd.AllowSorting = False

Response.AddHeader("content-disposition", "attachment;filename=Exp.xls")

Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False

Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)

Dim frm As HtmlForm = New HtmlForm()
Dim grd As New GridView

Me.Controls.Add(frm)
frm.Controls.Add(gd)

frm.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()


When i 'programatically create' the datasouce i am running into a problems. Though i can loop thorough the columns making them visible no data shows just blank cells. If the gridview is tied to Sqldatasource this is solved by using databind, when the data source is progrmatically create the databind throws some html into excel (<form name="aspnetForm" method="post" action="SalesSizeAnlaysis.aspx" id="aspnetForm"> etc).


All suggestions appreciated.

Stephen
 
i think this is similar, perform your gridview style changes early (column visible)(its what u are doing), and i think the Response.Clear() and Response.Buffer = True will help

Code:
    Sub ExportDG(ByVal sender As Object, ByVal e As ImageClickEventArgs)
        'Have to disable sorting while the object is being exported to Excel
        dgReport.AllowSorting = False
        dgReport.AllowPaging = False
        dgReport.Width = Unit.Pixel(1200)
        dgReport.GridLines = 1
        dgReport.HeaderStyle.BackColor = System.Drawing.Color.White
        dgReport.ItemStyle.BackColor = System.Drawing.Color.White
        'Rebind the data to be loaded and set back to standard sorting
        showReport(ViewState("repID"))

        Dim strFileName As String = ViewState("repTitle") & ".xls"

        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment; filename=""" & strFileName & """")
        Response.ContentEncoding = System.Text.Encoding.UTF7
        Response.Charset = ""
        EnableViewState = False
        Dim oStringWriter As New System.IO.StringWriter
        Dim oHTMLTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)

        dgReport.RenderControl(oHTMLTextWriter)
        Response.Write(oStringWriter.ToString())
        Response.End()
    End Sub
 
Have tried the buffer and clear but no positive out come.

I think my issue is in the binding, the button is on a master page i'm tring to make quite i generic export, when the datasource is built it doesn't know what to bind back to so returns nothing good. Your code calls showReport(ViewState("repID")) which i would guess is binding sub of the form. Are there any alternatives.

Cheers

Stephen
 
you will have to rebind your gridview after applying the property changes. otherwise nothing good.

youll have to generalize your datasource then too.

a few more lines and you are set (maybe)

gd.DataSource = myGeneralDS("GridView1")
gd.DataBind

Function myGeneralDS(sentGV As String) As DataSet
If sentGV = "GridView1" Then
Dim sqlCon As New SqlConnection(conString)
Dim sqlCom As New SqlDataAdapter("gv_1", sqlCon)
Dim ds As New DataSet()
sqlCom.Fill(ds)
myGeneralDS = ds
ElseIf sentGV = "GridView2"
'change your params
End If
End Function
 
Cheers, have a solution though i back tracked a little.

I had to build my datasourse as some of the parametes being passed to the stored procedure need a bit of formating ( check box list with multiple values), any how i end up formatting these into non-visible text boxes and linked the stored procedure to these. This allows me to use the converntional databind rather than a specific one i create myself.

Form and export now working. Thanks

Stephen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top