Page 2 of 2

Re: Can't get datagrid to export to CSV

Posted: Thu Feb 11, 2016 7:14 am
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.

Re: Can't get datagrid to export to CSV

Posted: Thu Feb 11, 2016 7:18 am
by jvierra
Here it is again:

Re: Can't get datagrid to export to CSV

Posted: Thu Feb 11, 2016 7:39 am
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.

Re: Can't get datagrid to export to CSV

Posted: Thu Feb 11, 2016 7:50 am
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.

Re: Can't get datagrid to export to CSV

Posted: Thu Feb 11, 2016 2:14 pm
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. }

Re: Can't get datagrid to export to CSV

Posted: Thu Feb 11, 2016 4:17 pm
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.

Re: Can't get datagrid to export to CSV

Posted: Thu Feb 11, 2016 4:31 pm
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.

Re: Can't get datagrid to export to CSV

Posted: Thu Feb 11, 2016 4:58 pm
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!

Re: Can't get datagrid to export to CSV

Posted: Thu Feb 11, 2016 5:59 pm
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.