Solved

How do I export datagridview to csv with powershell?

Posted on 2013-11-17
5
2,946 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 69

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 69

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 69

Expert Comment

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

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

735 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