Solved

How do I export datagridview to csv with powershell?

Posted on 2013-11-17
5
2,690 Views
1 Endorsement
Last Modified: 2013-11-19
Hi all,

I'm writing an app using powershell studio, one of the functions grabs a list of user accts from AD, and populates a datagridview, the user can than modify the data in that dgv. I'm having a very rough time figuring out how to write the dgv out to a csv file once the changes a re made.  Any help would be very much appreciated.

Thanks.
1
Comment
Question by:darksun666
  • 3
  • 2
5 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
Looks like neither SAPIEN nor Microsoft delivers something ready to use for that purpose, and so you will have to build the CSV manually.
The following code is based on http://coastalcoder.wordpress.com/2012/11/07/export-datagridview-to-csv-file-with-c/ with some improvements:
function export-DGV2CSV ([Windows.Forms.DataGridView] $grid, [String] $File)
{
  if ($grid.RowCount -eq 0) { return } # nothing to do
  
  $row = New-Object Windows.Forms.DataGridViewRow
  $sw  = new-object System.IO.StreamWriter($File)
        
  # write header line
  $sw.WriteLine( ($grid.Columns | % { $_.HeaderText } ) -join ',' )

  # dump values
  $grid.Rows | % {
    $sw.WriteLine(
      ($_.Cells | % { $_.Value }) -join ','
      )
    }
  $sw.Close()
}

Open in new window

I have not been able to test that code with real data, as building a valid GridView is tedious without some GUI tools, but it should work in general. At least as long as you do not have commas, newline and such, which require some more work if present.
0
 

Author Closing Comment

by:darksun666
Comment Utility
You sir, are a gentleman and a scholar.  much easier than what I was trying to do!
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
That's the "beauty" of PowerShell - being able to "stream" objects makes it much easier to iterate thru collections than in VB or C#.
0
 

Author Comment

by:darksun666
Comment Utility
I was directed to this bit o code, it works a treat, even eliminating comma issues..

$datagridview1.Rows |
     select -expand DataBoundItem |
     export-csv test.csv -NoType

Open in new window


thanks to jvierra at Sapien for that snippet!
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Thanks for posting that!
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

One of the features I've come to appreciate about Windows 7 and Windows Server 2008 R2 is the ability to pin applications to the task bar. As useful a feature as I've found this, it does have some quirks.  For example, have you ever tried pinning an…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now