In my application, I have a datagridview that looks like this (2 columns displaying info with no row header, no column header) :
PROPERTY | VALUE
PROPERTY | VALUE
PROPERTY | VALUE
PROPERTY | VALUE
PROPERTY | VALUE
Which is created by multiple :
$datagridviewInfo.Rows.Add("PropertyX", $ValueX)
Once the script ran and all the data is displayed, I'm simply trying to export all these rows to a CSV file.
I'm trying :
$datagridviewInfo.Rows | Select-Object -ExpandProperty DataBoundItem | Export-Csv InfoPoste.csv -NoTypeInformation -Force
but it returns a completly empty file, the property DataBoundItem doesn't seem to contain anything.
How should I approach this?
Thanks
Export Datagridview 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: Export Datagridview to CSV
In my case since the grid is created from a lot of different variables using :
$datagridviewInfo.Rows.Add(" ", " ")
Is there a way to create a new DataTable FROM the already populated rows??
Re: Export Datagridview to CSV
See the following example:
- Attachments
-
- Demo-DGVExport.psf
- (15.13 KiB) Downloaded 396 times
Re: Export Datagridview to CSV
Since the data is already generated with a different method than from a DataTable, my question is more like :
How do I create a DataTable from an EXISTING DataGridView?
So far I have :
$DataTableCSV = New-Object System.Data.DataTable
$DataTableCSV.TableName = "DataTableCSV"
$DataTableCSV.Columns.Add("Propriété")
$DataTableCSV.Columns.Add("Valeur")
foreach ($row in $datagridviewInfo.Rows)
{
foreach ($cell in $row.Cells[0])
{
$cell = $cell.Value.ToString()
$DataTableCSV.Rows.Add($cell)
}
}
but that is only giving me the cells from column one, i'm struggling with the logic to do a foreach which would add all cells from column one and all cells from column two...
How do I create a DataTable from an EXISTING DataGridView?
So far I have :
$DataTableCSV = New-Object System.Data.DataTable
$DataTableCSV.TableName = "DataTableCSV"
$DataTableCSV.Columns.Add("Propriété")
$DataTableCSV.Columns.Add("Valeur")
foreach ($row in $datagridviewInfo.Rows)
{
foreach ($cell in $row.Cells[0])
{
$cell = $cell.Value.ToString()
$DataTableCSV.Rows.Add($cell)
}
}
but that is only giving me the cells from column one, i'm struggling with the logic to do a foreach which would add all cells from column one and all cells from column two...
Re: Export Datagridview to CSV
You need to start with a DataTable. Why is that so hard? Just convert your object collection to a DataTable.
Post the code that loads your grid and I will give you an example.
Post the code that loads your grid and I will give you an example.
Re: Export Datagridview to CSV
I now see my mistake was to just use rows.add to display the data and not put these rows in a data table. I'll change my code so it build a data table.
Right now it's checking a big list of checkboxes. If a checkbox is checked, it runs a command and adds a row.
I guess I just need to add my rows to the datatable object I'll create and then I can export it easily !
Re: Export Datagridview to CSV
Ok I figured it out. My goal by the way is to see the rows being added in real time, I have a lot of variables and don't want to build the whole datatable and THEN show it in the datagridview, so here is a condensed example of what i'm doing (which is actually a lot more complex over a few hundred lines of code). It works as expected and the export-csv is working as well. In the example i added sleep to simulate a command taking some time to complete.
Now, am I approaching this in the right way with the Refresh method or is there an optimal way of doing this differently? I found a way which works but would like to know if it's proper before I implement it with my dozens of commands and variables. Thanks a lot for your time.
$button1_Click={
$table = New-Object system.Data.DataTable "TableInfoPoste"
$table.Columns.Add("Propriété")
$table.Columns.Add("Valeur")
$datagridview1.DataSource = $table
$datagridview1.ColumnHeadersVisible = $false
$datagridview1.RowHeadersVisible = $false
$a = "aaaa"
$table.Rows.add("a", $a)
$datagridview1.Refresh()
Start-Sleep 1
$b = "bbbb"
$table.Rows.add("b", $b)
$datagridview1.Refresh()
Start-Sleep 1
$c = "cccc"
$table.Rows.add("c", $c)
$datagridview1.Refresh()
$datagridview1.Rows | Select-Object -ExpandProperty DataBoundItem | Export-Csv C:\temp\InfoPoste.csv -NoTypeInformation -Encoding UTF8 -Force
}
Now, am I approaching this in the right way with the Refresh method or is there an optimal way of doing this differently? I found a way which works but would like to know if it's proper before I implement it with my dozens of commands and variables. Thanks a lot for your time.
$button1_Click={
$table = New-Object system.Data.DataTable "TableInfoPoste"
$table.Columns.Add("Propriété")
$table.Columns.Add("Valeur")
$datagridview1.DataSource = $table
$datagridview1.ColumnHeadersVisible = $false
$datagridview1.RowHeadersVisible = $false
$a = "aaaa"
$table.Rows.add("a", $a)
$datagridview1.Refresh()
Start-Sleep 1
$b = "bbbb"
$table.Rows.add("b", $b)
$datagridview1.Refresh()
Start-Sleep 1
$c = "cccc"
$table.Rows.add("c", $c)
$datagridview1.Refresh()
$datagridview1.Rows | Select-Object -ExpandProperty DataBoundItem | Export-Csv C:\temp\InfoPoste.csv -NoTypeInformation -Encoding UTF8 -Force
}
Re: Export Datagridview to CSV
Just add rows to the DataTable as you need. Once bound to the grid they will show automatically. I have used a grid for a dynamic logger and to dynamically display the results of a job as they arrived.