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.
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
raphaelgj
Posts: 23
Last visit: Thu Aug 17, 2023 8:41 am

Export Datagridview to CSV

Post by raphaelgj »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Export Datagridview to CSV

Post by jvierra »

You have to load the grid from a DataTable in order to have a "data bound" row.
User avatar
raphaelgj
Posts: 23
Last visit: Thu Aug 17, 2023 8:41 am

Re: Export Datagridview to CSV

Post by raphaelgj »

jvierra wrote: Fri Nov 16, 2018 11:33 am 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??
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Export Datagridview to CSV

Post by jvierra »

No. You need to bind the table when you load the grid. Why is that a problem?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Export Datagridview to CSV

Post by jvierra »

See the following example:
Attachments
Demo-DGVExport.psf
(15.13 KiB) Downloaded 394 times
User avatar
raphaelgj
Posts: 23
Last visit: Thu Aug 17, 2023 8:41 am

Re: Export Datagridview to CSV

Post by raphaelgj »

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...
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Export Datagridview to CSV

Post by jvierra »

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: 23
Last visit: Thu Aug 17, 2023 8:41 am

Re: Export Datagridview to CSV

Post by raphaelgj »

jvierra wrote: Fri Nov 16, 2018 12: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: 23
Last visit: Thu Aug 17, 2023 8:41 am

Re: Export Datagridview to CSV

Post by raphaelgj »

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


}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Export Datagridview to CSV

Post by jvierra »

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.
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked