Solved

How do I export datagridview to csv with powershell?

Posted on 2013-11-17
5
3,102 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39654708
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
ID: 39654752
You sir, are a gentleman and a scholar.  much easier than what I was trying to do!
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39654767
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
ID: 39660326
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 70

Expert Comment

by:Qlemo
ID: 39661012
Thanks for posting that!
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
While working, an annoying popup showing below will come and we cannot cancel or close it form the screen. The error message will come again and again.
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
This Micro Tutorial will teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.

630 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