Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 371085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:04:50+00:00 2026-05-12T14:04:50+00:00

These routines (vb.net) allow you to dump a gridview to CSV, even if there

  • 0

These routines (vb.net) allow you to dump a gridview to CSV, even if there are templated controls in the cells. It works, but I’m not thrilled with it.

What improvements should I make and why?

Private Shared Function CsvFormatted(ByVal t As String) As String
    If t.Contains(",") Then
        t = """" + t + """"
    End If
    Return t.Replace("\ ", "")

End Function

Private Shared Function GetCellText(ByVal cell As DataControlFieldCell) As String
    If cell.Controls.Count = 0 Then
        Return CsvFormatted(cell.Text)
    Else
        For Each current In cell.Controls
            If TypeOf current Is Label Then
                Return CsvFormatted(TryCast(current, Label).Text)
            ElseIf TypeOf current Is TextBox Then
                Return CsvFormatted(TryCast(current, TextBox).Text)
            ElseIf TypeOf current Is LinkButton Then
                Return CsvFormatted(TryCast(current, LinkButton).Text)
            ElseIf TypeOf current Is ImageButton Then
                Return CsvFormatted(TryCast(current, ImageButton).AlternateText)
            ElseIf TypeOf current Is HyperLink Then
                Return CsvFormatted(TryCast(current, HyperLink).Text)
            ElseIf TypeOf current Is DropDownList Then
                Return CsvFormatted(TryCast(current, DropDownList).SelectedItem.Text)
            ElseIf TypeOf current Is CheckBox Then
                Return CsvFormatted(If(TryCast(current, CheckBox).Checked, "True", "False"))
            End If
        Next
    End If
    Return ""
End Function

Public Shared Sub ExportGridViewToCSV(ByVal grid As GridView, ByVal fileName As String)
    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.Buffer = True
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName)
    HttpContext.Current.Response.Charset = ""
    HttpContext.Current.Response.ContentType = "application/text"
    Dim sb As New StringBuilder()
    For k As Integer = 0 To grid.Columns.Count - 1
        grid.Columns(k).Visible = True
        'add separator
        sb.Append(grid.Columns(k).HeaderText + ","c)
    Next
    'append new line
    sb.Append(vbCr & vbLf)
    For i As Integer = 0 To grid.Rows.Count - 1
        For k As Integer = 0 To grid.Columns.Count - 1
            grid.Columns(k).Visible = True
            'add separator
            sb.Append(GetCellText(grid.Rows(i).Cells(k)) + ","c)
        Next
        'append new line
        sb.Append(vbCr & vbLf)
    Next
    HttpContext.Current.Response.Output.Write(sb.ToString())
    HttpContext.Current.Response.Flush()
    HttpContext.Current.Response.End()
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:04:51+00:00Added an answer on May 12, 2026 at 2:04 pm
    • Should you be worrying about unescaping other HTML literals besides non-breaking space? You could use HttpUtility.HtmlDecode.
    • Your CsvFormatted routine could guard against null input strings – it costs almost nothing to be safe.
    • Do you need to pass the Turkey test? Some countries use a semicolon as the CSV delimiter. You might also need to account for dots or commas as decimal delimiter.
    • I might separate out building a CSV string into a separate function from issuing an HttpContext response.
    • I might use vbCrLf rather than vbCr & vbLf.

    My main advice: create some good unit tests, make sure the code passes, then forget it, and move on to implement some more functionality. The code is fairly well encapsulated, so you can easily refactor in future if you ever decide it’s necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to enabling collapsing of functions and sub-routines for Classic ASP
These ribbon in stackoverflow and uservoice.com looks nice. is there any javascript library/jquery plugin
These days, more languages are using unicode, which is a good thing. But it
These two may look like they have no correlation but bear with me! In
Do these two keywords have exactly the same effect, or is there something I
I have created a VB.NET Class Library that exposes some COM Interop sub routines.
If an application uses the java.net.* routines, I can set a proxy when invoking
In .NET there is the null reference, which is used everywhere to denote that
In .NET there is the null reference, which is used everywhere to denote that
I am slowly learning how to use Reactive Extensions for .NET with WPF. There

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.