Support Forums

Can't get datagrid to export to CSV

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.
User avatar
WEAVER84
Posts: 9
Joined: Wed Feb 10, 2016 2:50 pm

Re: Can't get datagrid to export to CSV

Postby WEAVER84 » Thu Feb 11, 2016 8:14 am

I'm not seeing in your example how you are, as you just said I need to do, use objects and update the data in an underlying dataset.
User avatar
jvierra
Posts: 9725
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Can't get datagrid to export to CSV

Postby jvierra » Thu Feb 11, 2016 8:18 am

Here it is again:
Attachments
Demo-DGVExport.psf
(13.84 KiB) Downloaded 118 times
User avatar
WEAVER84
Posts: 9
Joined: Wed Feb 10, 2016 2:50 pm

Re: Can't get datagrid to export to CSV

Postby WEAVER84 » Thu Feb 11, 2016 8:39 am

Yep, that is the example I had questioned about... Here is what I was questioning again:

WEAVER84 wrote:I'm not seeing in your example how you are, as you just said I need to do, use objects and update the data in an underlying dataset.
User avatar
jvierra
Posts: 9725
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Can't get datagrid to export to CSV

Postby jvierra » Thu Feb 11, 2016 8:50 am

Ta
he datasource is built as a an object collection and attached. You can build an object collection and attach it the way I do in the Load event.
User avatar
WEAVER84
Posts: 9
Joined: Wed Feb 10, 2016 2:50 pm

Re: Can't get datagrid to export to CSV

Postby WEAVER84 » Thu Feb 11, 2016 3:14 pm

That still didn't make sense to me, not seeing how to add data to the object. But figured out another way (probably much more complicated :) )

Here is button to ExportToCSV and function that loads it into a CSV for anyone else who is wondering:

#button to Export to CSV
$buttonExportToCSV_Click = {
#promp to choose folder location
$folderbrowsermoderndialog1.InitialDirectory = "C:\"
 
if ($folderbrowsermoderndialog1.ShowDialog() -eq 'OK')
{
#saves the selected folder path
$SelectedPath = $folderbrowsermoderndialog1.SelectedPath
}
 
#removes any special characters from searched keyword (the keyword was used to find values)
$RemoveSpecialCharsFromTitle = ($($textbox1.Text) -replace '[\W]', '')
 
#store full file path name into $export
$export = "$SelectedPath\$($RemoveSpecialCharsFromTitle)_Report.csv"
 
#export to csv
export-DGV2CSV $DataGridView $export
}
 
#Export DataGrid to CSV
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()
}
User avatar
jvierra
Posts: 9725
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Can't get datagrid to export to CSV

Postby jvierra » Thu Feb 11, 2016 5:17 pm

there is really no need to do all of that. If you set the DGV up correctly then it will export with one line.
User avatar
jvierra
Posts: 9725
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Can't get datagrid to export to CSV

Postby jvierra » Thu Feb 11, 2016 5:31 pm

If you create objects you can add them the grid. When you want to save then you will have an exportable datasounrce.

There are two ways to do this. The best way to to use a datatable as it is most easily extendanle.

$dt.New-Object System.Data.Datatable
$datgridview1.DataSource=$dt
$column = new System.Data.DataColumn();
$column.DataType = System.Type.GetType("System.Int32");
$column.ColumnName = "id";
$dt.Columns.Add($column);
 
 
Now we add columns and rows.
$row-$dt.NewRow()
$row.ID=1
$dt.Rows.Add($row)


Keep adding rows as needed or delete nd edit rows. When done it is exportable. It is also the only way to get the grid to sort by clicking on the column header.

If I get a bit of time later I will build a small working example.
User avatar
WEAVER84
Posts: 9
Joined: Wed Feb 10, 2016 2:50 pm

Re: Can't get datagrid to export to CSV

Postby WEAVER84 » Thu Feb 11, 2016 5:58 pm

I'll have to play around with that datatable. That looks like probably what I should've been doing. I'll do some playing around with that tomorrow. But definitely looks like what I need to learn. Super new to this :) Thank you!
User avatar
jvierra
Posts: 9725
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Can't get datagrid to export to CSV

Postby jvierra » Thu Feb 11, 2016 6:59 pm

Will wonders never cease? forgot I had built a table demo quite a while ago.

There are two file(forms). Save both to the same folder. The Demo-DGVDatatabel.psf is the master. Open it and run it. The form is self explanatory. Look how easily it adds rows and edits in the grip. There is almost no code to do all of this.


Notice how easy it is to generate a new row and allow a simple form to edit it.

Also note that the grid is delete-able and full sortable by clicking on the column headers. It lacks a validation scheme, an error manager and some mechanism to save its configuration. We can also persist the data table to almost any database.
Attachments
Demo-DGVDataTable.psf
(37.52 KiB) Downloaded 101 times
New-PersonForm.psf
(38.16 KiB) Downloaded 100 times

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: No registered users and 1 guest