Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

Exporting data to CSV from Gridview which is populated by dropdownlists

$
0
0

I'm struggling to know how to accomplish the following;

I have a gridview which is populated depending on choices from two drop down lists. This aspect works fine and the gridview displays the data and refreshes if a different values are chosen, however I'm struggling to then export whatever is currently displaying in the gridview. Currently an export to csv is simply blank. Now I'm pretty sure this is because the button event is tagged to the page load which means the gridview is empty at that point but I'm unsure how to adapt the following VB.net code to bound it to the current bound data in the gridview when the button event occurs.

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition","attachment;filename=GridViewExport.csv")
        Response.Charset = ""
        Response.ContentType = "application/text"

        GridView1.AllowPaging = True
        GridView1.DataBind()

        Dim sb As New StringBuilder()
        For k As Integer = 0 To GridView1.Columns.Count - 1
            'add separator
            sb.Append(GridView1.Columns(k).HeaderText + ","c)
        Next
        'append new line
        sb.Append(vbCr & vbLf)
        For i As Integer = 0 To GridView1.Rows.Count - 1
            For k As Integer = 0 To GridView1.Columns.Count - 1
                'add separator
                sb.Append(GridView1.Rows(i).Cells(k).Text + ","c)
            Next
            'append new line
            sb.Append(vbCr & vbLf)
        Next
        Response.Output.Write(sb.ToString())
        Response.Flush()
        Response.End()
    End Sub

Any pointers or guidance would be much appreciated.

Thanks, Paul.


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>