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.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
WEAVER84
Posts: 9
Last visit: Thu Feb 18, 2016 1:54 pm

Re: Can't get datagrid to export to CSV

Post by WEAVER84 »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Can't get datagrid to export to CSV

Post by jvierra »

Here it is again:
Attachments
Demo-DGVExport.psf
(13.84 KiB) Downloaded 256 times
User avatar
WEAVER84
Posts: 9
Last visit: Thu Feb 18, 2016 1:54 pm

Re: Can't get datagrid to export to CSV

Post by WEAVER84 »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Can't get datagrid to export to CSV

Post by jvierra »

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
Last visit: Thu Feb 18, 2016 1:54 pm

Re: Can't get datagrid to export to CSV

Post by WEAVER84 »

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:
  1. #button to Export to CSV
  2. $buttonExportToCSV_Click = {
  3.     #promp to choose folder location
  4.     $folderbrowsermoderndialog1.InitialDirectory = "C:\"
  5.    
  6.     if ($folderbrowsermoderndialog1.ShowDialog() -eq 'OK')
  7.     {
  8.         #saves the selected folder path
  9.         $SelectedPath = $folderbrowsermoderndialog1.SelectedPath
  10.     }
  11.    
  12.     #removes any special characters from searched keyword (the keyword was used to find values)
  13.     $RemoveSpecialCharsFromTitle = ($($textbox1.Text) -replace '[\W]', '')
  14.    
  15.     #store full file path name into $export
  16.     $export = "$SelectedPath\$($RemoveSpecialCharsFromTitle)_Report.csv"
  17.    
  18.     #export to csv
  19.     export-DGV2CSV $DataGridView $export
  20. }
  21.  
  22. #Export DataGrid to CSV
  23. function export-DGV2CSV ([Windows.Forms.DataGridView]$grid, [String]$File)
  24. {
  25.     if ($grid.RowCount -eq 0) { return } # nothing to do
  26.    
  27.     $row = New-Object Windows.Forms.DataGridViewRow
  28.     $sw = new-object System.IO.StreamWriter($File)
  29.    
  30.     # write header line
  31.     $sw.WriteLine(($grid.Columns | % { $_.HeaderText }) -join ',')
  32.    
  33.     # dump values
  34.     $grid.Rows | % {
  35.         $sw.WriteLine(
  36.         ($_.Cells | % { $_.Value }) -join ','
  37.         )
  38.     }
  39.     $sw.Close()
  40. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Can't get datagrid to export to CSV

Post by jvierra »

there is really no need to do all of that. If you set the DGV up correctly then it will export with one line.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Can't get datagrid to export to CSV

Post by jvierra »

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.
  1. $dt.New-Object System.Data.Datatable
  2. $datgridview1.DataSource=$dt
  3. $column = new System.Data.DataColumn();
  4. $column.DataType = System.Type.GetType("System.Int32");
  5. $column.ColumnName = "id";
  6. $dt.Columns.Add($column);
  7.  
  8.  
  9. Now we add columns and rows.
  10. $row-$dt.NewRow()
  11. $row.ID=1
  12. $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
Last visit: Thu Feb 18, 2016 1:54 pm

Re: Can't get datagrid to export to CSV

Post by WEAVER84 »

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!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Can't get datagrid to export to CSV

Post by jvierra »

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 235 times
New-PersonForm.psf
(38.16 KiB) Downloaded 265 times
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked