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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:27:19+00:00 2026-05-15T06:27:19+00:00

Is there a simple way to export a WPF DataGrid to a text file

  • 0

Is there a simple way to export a WPF DataGrid to a text file and a .csv file? I’ve done some searching and can’t see that there is a simple DataGrid method to do so. I have got something working (barely) by making use of the ItemsSource of the DataGrid. In my case, this is an array of structs. I do something with StreamWriters and StringBuilders (details available) and basically have:

StringBuilder destination = new StringBuilder();
destination.Append(row.CreationDate);
destination.Append(seperator);  // seperator is '\t' or ',' for csv file
destination.Append(row.ProcId);
destination.Append(seperator);
destination.Append(row.PartNumber);
destination.Append(seperator);

I do this for each struct in the array (in a loop). This works fine. The problem is that it’s not easy to read the text file. The data can be of different lengths within the same column. I’d like to see something like:
2007-03-03 234238423823 823829923
2007-03-03 66 99
And of course, I get something like:
2007-03-03 234238423823 823829923
2007-03-03 66 99

It’s not surprising giving that I’m using Tab delimiters, but I hope to do better. It certainly is easy to read in the DataGrid! I could of course have some logic to pad short values with spaces, but that seems quite messy. I thought there might be a better way. I also have a feeling that this is a common problem and one that has probably been solved before (hopefully within the .NET classes themselves).

Thanks.

  • 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-15T06:27:20+00:00Added an answer on May 15, 2026 at 6:27 am

    I assume the goal is to be as WYSIWYG as possible. In that case I would:

    1. Loop through all DataGridRows, and for each one build a string[] containing the data displayed in each DataGridCell. This is easy for most DataGridColumn types. For DataGridTemplateColumn you’ll need to use the Binding then convert .ToString().
    2. I also would keep track of the maximum string length in each column as I did so.

    The next step depends on the export type:

    • For a csv export I would then append all the strings in each string[] inside quotes with commas between them and write them out.
    • For a plain text export I would append all the strings in each string[] padded with spaces to 1+ the maximum string length for that column and write them out.

    The reason to use the actual DataGridRow, DataGridCell and DataGridColumn objects is to write out exactly what the end-user sees. For example you can skip hidden columns, apply string formats in binding, honor filtering/grouping/sorting, etc.

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

Sidebar

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.