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
Solved

How do I export datagridview to csv with powershell?

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Article by: Lee
Windows 7 Ultimate and Enterprise (and 2008 R2) introduced a new feature you may not be aware of - Boot from VHD.   Boot from VHD (or what Microsoft refers to asNative Boot allows you to install Windows to a VHD (Virtual Hard Disk) file that is t…
By default the complete memory dump option is disabled in windows . If we want to enable the complete memory dump for a diagnostic purpose, we have a solution for it. here we are using the registry method to enable this.
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.
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…

861 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