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 7897451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:10:11+00:00 2026-06-03T08:10:11+00:00

I have been trying to export some datagrids in Silverlight to excel, but have

  • 0

I have been trying to export some datagrids in Silverlight to excel, but have been unsuccesful so far. So now I am just trying to get them to export as a csv file that can then be opened in excel. I have a class created to export the datagrids and it does export and save a csv file. However, the csv file contains no data.

Imports System.IO

Public Class clsExportData
   'Public Shared Sub Export(dg As DataGrid)
   '    SaveExportedGrid(ExportDataGrid(True, dg))
   'End Sub


Public Shared Sub Export(dg As DataGrid, withHeaders As Boolean)
    SaveExportedGrid(ExportDataGrid(withHeaders, dg))
End Sub

Private Shared Sub SaveExportedGrid(data As String)
    Dim sfd As New SaveFileDialog()
    sfd.DefaultExt = "csv"
    sfd.Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*"
    sfd.FilterIndex = 1

    If If(sfd.ShowDialog(), False) Then
        Using sr As New StreamWriter(sfd.OpenFile())
            sr.Write(data)
        End Using
    End If
End Sub

Private Shared Function ExportDataGrid(withHeaders As Boolean, grid As DataGrid) As String
    Dim colPath As String
    Dim propInfo As System.Reflection.PropertyInfo
    Dim binding As System.Windows.Data.Binding
    Dim strBuilder As New System.Text.StringBuilder()
    Dim source As System.Collections.IList = TryCast(grid.ItemsSource, System.Collections.IList)
    If source Is Nothing Then
        Return ""
    End If

    Dim headers As New List(Of String)()
    grid.Columns.ToList().ForEach(Function(col)
                                      If TypeOf col Is DataGridBoundColumn Then
                                          headers.Add(FormatCSVField(col.Header.ToString()))
                                      End If

                                  End Function)
    strBuilder.Append([String].Join(",", headers.ToArray())).Append(vbCr & vbLf)

    For Each data As [Object] In source
        Dim csvRow As New List(Of String)()
        For Each col As DataGridColumn In grid.Columns
            If TypeOf col Is DataGridBoundColumn Then
                binding = TryCast(col, DataGridBoundColumn).Binding
                colPath = binding.Path.Path
                propInfo = data.[GetType]().GetProperty(colPath)
                If propInfo IsNot Nothing Then
                    csvRow.Add(FormatCSVField(propInfo.GetValue(data, Nothing).ToString()))
                End If
            End If
        Next
        strBuilder.Append([String].Join(",", csvRow.ToArray())).Append(vbCr & vbLf)
    Next

    Return strBuilder.ToString()
End Function

Private Shared Function FormatCSVField(data As String) As String
    Return [String].Format("""{0}""", data.Replace("""", """""""").Replace(vbLf, "").Replace(vbCr, ""))
End Function

End Class

  • 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-06-03T08:10:12+00:00Added an answer on June 3, 2026 at 8:10 am

    Try the following instead

    Dim source As System.Collections.IEnumerable = TryCast(grid.ItemsSource, System.Collections.IEnumerable)
    If source Is Nothing Then
        Return ""
    End If
    

    Also, make sure you step through your code with a debugger, and see where it failed.

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

Sidebar

Related Questions

I have been trying to export the content of mysql database to excel using
For about a week now I have been trying to get a view to
I have been trying all afternoon to get the jQuery Sifr Plugin ( http://jquery.thewikies.com/sifr/
I have been trying to understand the way ActionScript's events are implemented, but I'm
I have been trying to figure out a solution but nothing has really presented
I've been trying this for quite a while now, but can't figure it out.
I have been trying to export a mysql table with INTO OUTFILE to CSV
I'm trying to export a record set into Excel but it seems to keep
I've been trying to find some tutorials on writing hook scripts, and so far
I have been trying to export a Word document into Response using ASP.Net. So

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.