Page 2 of 2

Re: Can't get datagrid to export to CSV

Posted: Thu Feb 11, 2016 8: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 8:18 am
by jvierra
Here it is again:

Re: Can't get datagrid to export to CSV

Posted: Thu Feb 11, 2016 8: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 8: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 3: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:

Code: Select all

#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

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

Code: Select all

$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.

Re: Can't get datagrid to export to CSV

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