Datagridview - Pasting from Excel Help

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.
Posts: 31
Joined: Tue Apr 28, 2020 4:07 am

Datagridview - Pasting from Excel Help

Post by ClipperXTP »

I am using jvierra's excellent earlier post to copy data from Excel to a Datagridview from here:


I have created a datagridview in the Designer.
I have not added or defined any columns.
My excel data has no headers.
The first column in spreadsheet is machine name
The second column is machine model
The data copies into the Datagridview fine. No problem.
I just can't do anything with it. I get white-space when I try to write-host a simple test and cannot operate on null values errors when I try to do anything:

#TODO: Place custom script here

$row = $datagridviewMachines.SelectedRows
foreach ($cell in $row.Cells)

$Name = $row.Cells['Column1'].Value
Write-Host $($Name)
$Model = $row.Cells['Column2'].Value
Write-Host $($Model)



Any ideas what I am doing wrong?

Posts: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times

Re: Datagridview - Pasting from Excel Help

Post by jvierra »

The spreadsheet must have headers or you must predefine the columns.