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

Datagridview - Pasting from Excel Help

Post by ClipperXTP »

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

viewtopic.php?t=9885

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:

$buttonTest_Click={
#TODO: Place custom script here

$datagridviewMachines.SelectAll()
$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?
thanks

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

Re: Datagridview - Pasting from Excel Help

Post by jvierra »

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

Locked