none
Expostando Grid View com AutoGenereteColums = True RRS feed

  • Pergunta

  • Tenho o seguinte método a baixo funcionando perfeitamente com AutoGenereteColums = false, daí tenho que incluir as colunas no grid! Porém está errado ele tem que exportar com 

    AutoGenereteColums = True e sem criar as colunas no grid. Como alterar o método a baixo?

       Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    
            Response.Clear()
            Response.Buffer = True
    
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")
            Response.Charset = ""
            Response.ContentType = "application/vnd.ms-excel"
    
            Dim sw As New StringWriter()
            Dim hw As New HtmlTextWriter(sw)
    
    
            GridView1.AllowPaging = False
            GridView1.DataBind()
    
            'Change the Header Row back to white color
            GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF")
    
            'Apply style to Individual Cells
    
            For i As Integer = 0 To GridView1.Rows.Count - 1
                Dim row As GridViewRow = GridView1.Rows(i)
    
    
                'Change Color back to white
                row.BackColor = System.Drawing.Color.White
    
                'Apply text style to each Row
                row.Attributes.Add("class", "textmode")
    
                'Apply style to Individual Cells of Alternating Row
                If i Mod 2 <> 0 Then
                    row.Cells(0).Style.Add("background-color", "#C2D69B")
                    row.Cells(1).Style.Add("background-color", "#C2D69B")
                    row.Cells(2).Style.Add("background-color", "#C2D69B")
                    row.Cells(3).Style.Add("background-color", "#C2D69B")
                End If
            Next
            GridView1.RenderControl(hw)
    
            'style to format numbers to string
            Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"
            Response.Write(style)
            Response.Output.Write(sw.ToString())
            Response.Flush()
            Response.End()
    
        End Sub
    


    abraços

    quinta-feira, 31 de janeiro de 2013 17:37

Respostas

  • Consegui!

        Private Sub ExportaGridView()
            Dim attachment As String = "attachment; filename=Contacts.xls"
            Response.ClearContent()
            Response.AddHeader("content-disposition", attachment)
            Response.ContentType = "application/ms-excel"
            Dim sw As New StringWriter()
            Dim htw As New HtmlTextWriter(sw)
            GridView1.RenderControl(htw)
            Response.Write(sw.ToString())
            Response.[End]()
        End Sub



    quinta-feira, 31 de janeiro de 2013 18:23

Todas as Respostas

  • Alguém?
    quinta-feira, 31 de janeiro de 2013 18:14
  • Consegui!

        Private Sub ExportaGridView()
            Dim attachment As String = "attachment; filename=Contacts.xls"
            Response.ClearContent()
            Response.AddHeader("content-disposition", attachment)
            Response.ContentType = "application/ms-excel"
            Dim sw As New StringWriter()
            Dim htw As New HtmlTextWriter(sw)
            GridView1.RenderControl(htw)
            Response.Write(sw.ToString())
            Response.[End]()
        End Sub



    quinta-feira, 31 de janeiro de 2013 18:23