Can't get datagrid to export to CSV
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.
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.
Re: Can't get datagrid to export to CSV
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
Here it is again:
- Attachments
-
- Demo-DGVExport.psf
- (13.84 KiB) Downloaded 261 times
Re: Can't get datagrid to export to CSV
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
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:
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()
- }
Re: Can't get datagrid to export to CSV
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.
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.
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)
If I get a bit of time later I will build a small working example.
Re: Can't get datagrid to export to CSV
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
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.
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 238 times
-
- New-PersonForm.psf
- (38.16 KiB) Downloaded 266 times