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

Im porting some calculation routines from .Net to Java but there seem to be
These are for two separate codecademy exercises. They both pass me but I'm not
This is probably a simple question but I am not an ASP.NET developer and
There is a particular library (.net dll) we had used for cryptography. But it
I know there is a way to call Perl routines from C. As shown
In the vxWorks shell, there are a number of routines you can use to
These days I design some algorithms in python, but find first two greatest value
( Edit : to clarify, my main goal is concurrency, but not necessarily for
I am currently working on an asp.net web page with a GridView displaying a
I have created a VB.NET Class Library that exposes some COM Interop sub routines.

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.