export-dgv2csv.ps1
function export-DGV2CSV ([Windows.Forms.DataGridView] $grid, [String] $File)
<#
.SYNOPSIS
Export basic datagrid to CSV file
.PARAMETER grid
Datagrid object
.PARAMETER file
Path to CSV 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 ',' )
#Export contents
$grid.Rows | % {
$sw.WriteLine(
($_.Cells | % { $_.Value }) -join ','
)
}
$sw.Close()
}