ASP.net VB Write contents of gridview to text file

Hi. How would I write the contents of a a GridView in ASP.net VB,net code to a text file on the user's computer?
I need to gather only the data from the rows where the additional column is checked as in the code below

    Protected Sub Button_Load_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_Load.Click

        Try
            Dim gvr As GridViewRow
            Dim chk As CheckBox
            For i As Integer = 0 To GridView1.Rows.Count
                gvr = GridView1.Rows(i)
                chk = gvr.FindControl("chkSelected")
                If (chk.Checked) Then

                    Response.Write(GridView1.Rows(i).Cells(3).Text)
                End If
            Next
        Catch ex As Exception

        End Try

    End Sub

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gamarrojgqCommented:
Hi,

Forsecurity restrictions we cannot write that text file to the client's hard-drive, but you can allows the user to download the file, in the attached code the process will write the textfile and ask the users to download it.

Just be aware that the code assumes that your gridview1 have the columns defined and use the columns count to write a textfile with the column values delimited by an  space char, and intents to write ALL THE COLUMNS VALUES.

Hope that helps
Dim strFileName As String = "FileName.txt"
        Dim strLine As String
        Dim intCol As Integer

        Response.ContentType = "text/html"

        Try
            Dim gvr As GridViewRow
            Dim chk As CheckBox
            For i As Integer = 0 To GridView1.Rows.Count
                gvr = GridView1.Rows(i)
                chk = gvr.FindControl("chkSelected")
                If (chk.Checked) Then
                    strLine = ""
                    For intCol = 0 To GridView1.Columns.Count - 1
                        strLine &= GridView1.Rows(i).Cells(intCol).Text & " "
                    Next
                    Response.Output.WriteLine(strLine)
                End If
            Next
        Catch ex As Exception
        End Try

        Response.AppendHeader("content-disposition", "attachment; filename=" & strFileName)
        Response.End()

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
thanks a lot for the help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.