0

I have a reporting page I am working on and I need to get this gridview exported to CSV. I have gotten everything setup properly but now when I export it the key collumn data is empty while all others are there. No where in the code do I mess with this actual data but I do check it to set visibility of the row. Here are the 3 key functions.: also note that this gridview renders perfect with all the data its not until its getting exported that the "Assignment" column gets blanked.

Protected Sub gvWaranaty_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvWaranaty.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Visible = False
            If Not Request.QueryString("Region") = "" Then
                Dim y As String = e.Row.DataItem("Assignment")
                For Each z As String In x
                    'x is set on_load and populated with all the properties in QueryString(Region)
                    If y = z Then
                        e.Row.Visible = True
                    End If
                Next
            End If
        End If
    End Sub


    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click


        Response.Clear()
        Response.AddHeader("content-disposition", "attachment;filename=ExperationReport" + Date.Now + ".csv")
        Response.Charset = ""

        Response.ContentType = "application/vnd.xls"

        Dim htmlwrite As New System.Web.UI.HtmlTextWriter(CSV_EXPORT())

        Response.Write(CSV_EXPORT())
        Response.End()
    End Sub



    Protected Function CSV_EXPORT() As StringWriter
        Dim sw As StringWriter = New StringWriter()

        Dim ColCount As Integer = gvWaranaty.Columns.Count
        For i As Integer = 0 To (ColCount - 1) Step 1
            sw.Write(gvWaranaty.Columns(i))

            If i < ColCount - 1 Then
                sw.Write(",")
            End If
        Next
        sw.Write(sw.NewLine)

        For Each dr As System.Web.UI.WebControls.GridViewRow In gvWaranaty.Rows
            For i As Integer = 0 To (ColCount - 1) Step 1
                If dr.Visible = True Then
                    'If its not visible dont export it 
                    If Not dr.Cells.Item(i).Text = "" Then
                        sw.Write(dr.Cells.Item(i).Text)
                    End If
                    If i < ColCount - 1 Then
                        sw.Write(",")
                    End If
                End If
            Next
            sw.Write(sw.NewLine)
        Next
        sw.Close()

        Return sw

    End Function

please advise or point to an article that will shed some light on this...I'm baffled but then again it is monday morning :P.

Thanks in advance

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by MichaelWClark
0

Okay once again it seems that by just posting to DaniWeb the answer magically appears to me in the code. My issue here was everything BUT the assignment was a boundfield. The assignment was a Template field. This required me to check for controls and pull data that way. My code above changed to:

If Not dr.Cells.Item(i).Text = "" Then
                        sw.Write(dr.Cells.Item(i).Text)
                    Else
                        If dr.Cells.Item(i).HasControls = True Then
                            For Each cnt As Control In dr.Cells.Item(i).Controls
                                If cnt.GetType.ToString = "System.Web.UI.WebControls.Label" Then
                                    'if this is a TemplateField then the control will be a label for this report.
                                    Dim out As String = CType(cnt, System.Web.UI.WebControls.Label).Text
                                    sw.Write(out)
                                End If
                            Next
                        End If
                    End I

Hope this helps anyone that has similar issues :).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.