Exporting a Nested Gridview to .CSV

Joined
Oct 22, 2008
Messages
1
Reaction score
0
Hello All, I am new to ASP.NET and doing all of my coding in VB .NET 2.0

I had built a site with a simple Gridview which was exportable to a CSV file. I have changed this simple gridview to a nested gridview. I am only able to export the parent gridview to a CSV. Here is the code I used to use to export the parent gridview.

Code:
        Response.Buffer = True
        Response.BufferOutput = True
        System.Web.HttpContext.Current.Response.ContentEncoding = Encoding.UTF8
        System.Web.HttpContext.Current.Response.Charset = ""
        System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=test.csv")
        Me.EnableViewState = False
        Dim sqlinsert As String = ""
        sqlinsert += dg1.Columns(3).HeaderText + "," + dg1.Columns(4).HeaderText + "," + dg1.Columns(5).HeaderText + "," + dg1.Columns(6).HeaderText + "," + dg1.Columns(7).HeaderText + "," + dg1.Columns(8).HeaderText
        Response.Write(sqlinsert)

        StringBuilder object 
        Dim str As New StringBuilder()
        If ExportItems.SelectedValue = 1 Then 'Export just check Items
        ' Select the checkboxes from the GridView control
         For i As Integer = 0 To dg1.Rows.Count - 1
         Dim row As GridViewRow = dg1.Rows(i)
         Dim chkSelect As Boolean = CType(dg1.Rows(i).FindControl("Selected"), CheckBox).Checked
         If chkSelect Then
         sqlinsert = vbCr & vbLf
         Dim sqlvalues As String = ""
         If row.Cells(2).Text = "" Then
         sqlvalues += row.Cells(1).Text.Replace("''", "'") + "," + DirectCast(row.Cells(2).Controls(0), HyperLink).Text.Replace("''", "'").Replace(" ", "") + "," + row.Cells(3).Text.Replace("''", "'").Replace(" ", "") + "," + row.Cells(4).Text.Replace("''", "'").Replace(" ", "") + "," + row.Cells(5).Text.Replace("''", "'").Replace(" ", "") + "," + row.Cells(6).Text.Replace("''", "'").Replace(" ", "") + "," + row.Cells(7).Text.Replace("''", "'").Replace(" ", "") + "," + row.Cells(8).Text.Replace("''", "'").Replace(" ", "") 
         sqlinsert = sqlinsert + sqlvalues
         Response.Write(sqlinsert)
         Else
         sqlvalues += row.Cells(1).Text.Replace("''", "'").Replace(" ", "") + "," + row.Cells(2).Text.Replace("''", "'").Replace(" ", "") + "," + row.Cells(3).Text.Replace("''", "'").Replace(" ", "") + "," + row.Cells(4).Text.Replace("''", "'").Replace(" ", "") + "," + row.Cells(5).Text.Replace("''", "'").Replace(" ", "") + "," + row.Cells(6).Text.Replace("''", "'").Replace(" ", "") + "," + row.Cells(7).Text.Replace("''", "'").Replace(" ", "") + "," + row.Cells(8).Text.Replace("''", "'").Replace(" ", "")
         sqlinsert = sqlinsert + sqlvalues
         Response.Write(sqlinsert)
         End If
         End If
         Next

Basically it writes out dg1.cell(x) and then a comma for every cell in the row. The problem is that the child grid is called dg2, but I am unable to reference it in the same manner as I am the parent. The exported CSV file just needs to have every column in the parent grid and the child grid on the same line. Like:

parentcell1, parentcell2, parentcell3, childcell1, childcell2, childcell3

If I try to export just the headers, the parent works fine using something like

Code:
dg1.Columns(3).HeaderText + ","

but the child does not

Code:
dg2.Columns(1).HeaderText + ","

dg2 is undefined even though it is a child under dg1.

Thanks for any help you can provide.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top