Export Datagridview 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.
User avatar
raphaelgj
Posts: 17
Joined: Tue Jun 19, 2018 10:19 am

Export Datagridview to CSV

Post by raphaelgj » Fri Nov 16, 2018 12:28 pm

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

User avatar
jvierra
Posts: 12958
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Export Datagridview to CSV

Post by jvierra » Fri Nov 16, 2018 12:33 pm

You have to load the grid from a DataTable in order to have a "data bound" row.

User avatar
raphaelgj
Posts: 17
Joined: Tue Jun 19, 2018 10:19 am

Re: Export Datagridview to CSV

Post by raphaelgj » Fri Nov 16, 2018 12:46 pm

jvierra wrote:
Fri Nov 16, 2018 12:33 pm
You have to load the grid from a DataTable in order to have a "data bound" row.
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??

User avatar
jvierra
Posts: 12958
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Export Datagridview to CSV

Post by jvierra » Fri Nov 16, 2018 1:17 pm

No. You need to bind the table when you load the grid. Why is that a problem?

User avatar
jvierra
Posts: 12958
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Export Datagridview to CSV

Post by jvierra » Fri Nov 16, 2018 1:19 pm

See the following example:
Attachments
Demo-DGVExport.psf
(15.13 KiB) Downloaded 10 times

User avatar
raphaelgj
Posts: 17
Joined: Tue Jun 19, 2018 10:19 am

Re: Export Datagridview to CSV

Post by raphaelgj » Fri Nov 16, 2018 1:28 pm

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

User avatar
jvierra
Posts: 12958
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Export Datagridview to CSV

Post by jvierra » Fri Nov 16, 2018 1:35 pm

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.

User avatar
raphaelgj
Posts: 17
Joined: Tue Jun 19, 2018 10:19 am

Re: Export Datagridview to CSV

Post by raphaelgj » Fri Nov 16, 2018 2:24 pm

jvierra wrote:
Fri Nov 16, 2018 1:35 pm
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.

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 !

User avatar
raphaelgj
Posts: 17
Joined: Tue Jun 19, 2018 10:19 am

Re: Export Datagridview to CSV

Post by raphaelgj » Fri Nov 16, 2018 4:05 pm

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


}

User avatar
jvierra
Posts: 12958
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Export Datagridview to CSV

Post by jvierra » Fri Nov 16, 2018 4:10 pm

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.

Post Reply