Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I export datagridview to csv with powershell?

Posted on 2013-11-17
5
Medium Priority
?
3,221 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 71

Accepted Solution

by:
Qlemo earned 2000 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 71

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 71

Expert Comment

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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
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 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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

721 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