datagridview cell reading

Ask your PowerShell-related questions, including questions on cmdlet development!
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 6 years and 8 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
FSU-Systems
Posts: 8
Last visit: Wed Oct 27, 2021 7:53 am

datagridview cell reading

Post by FSU-Systems »

I am currently playing with datagridview for a script I am working on, having a list of active sessions from our VMware view environment into the Gridview. Got this functioning to where it loads the rows.

I have also been able to get the data from a selected Cell, but am having hard time figuring out how to take a selected row and read the data from each Column / Cell for that row.

I.E.

In the attached picture, there is a row with following columns

USERNAME, POOL, DesktopName, Duration, Server, Status, ID

I have figured out how to read if I select one of those cells and use that data for something else. but would like to be able to select the Row and then read each of the pieces of information for different things from just that selected Row, and if possible from multiple selected rows in case I want to send something to a group of sessions.

Such as grab that and store each item in its own Variable to be able to send a command to the Computername with the user that is logged in.

Thanks and hopefully this makes sense.
Attachments
vditool.PNG
vditool.PNG (2.45 KiB) Viewed 4555 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: datagridview cell reading

Post by jvierra »

Have you tried this:

foreach($cell in $row.Cells){
$cell.Value
}
User avatar
FSU-Systems
Posts: 8
Last visit: Wed Oct 27, 2021 7:53 am

Re: datagridview cell reading

Post by FSU-Systems »

Thanks for that, one followup question on it,

This is what I have on this button so far

$row = $datagridview1.SelectedRows
foreach ($cell in $row.Cells)
{
$cell.value
Write-Host $cell.value
}

When I click a test row, I get this output

Comp1
Test Pool
Comp1.edu
1 Hour
Server1
Connected
ID

But with the $cell, how I can differentiate basically to read Column1 and Column3 and such, as looking for different tasks I will need different pieces of the overall data, wouldn't need all the columns data each time
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: datagridview cell reading

Post by jvierra »

$row.Cells['Column1'].Value
User avatar
FSU-Systems
Posts: 8
Last visit: Wed Oct 27, 2021 7:53 am

Re: datagridview cell reading

Post by FSU-Systems »

Do not appear to be getting any output with that.

Tried the following

$row = $datagridview1.SelectedRows
Write-Host $row.Cells['Username'].Value
Write-Host $row.Cells['Column0'].Value
Write-Host $row.Cells['Column1'].Value

Username being what the column is named earlier in the script via

$dataGridView1.Columns[0].Name = "Username"

And All the write-hosts do not appear to be outputting any data, seems to be whitespace but nothing else

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

Re: datagridview cell reading

Post by jvierra »

You have to name your columns in the designer. A column has a header and a name. Both need to be set.
User avatar
FSU-Systems
Posts: 8
Last visit: Wed Oct 27, 2021 7:53 am

Re: datagridview cell reading

Post by FSU-Systems »

Thanks for that tip, I have changed it and got the Columns created via the designer and given their headers, and have commented out the part in the script that was doing it. But I still get white space when trying to get the data.

I did notice that when I created each column they showed the names they were getting Column1 through 7, but when looking in the Designed I do not see those names anywhere, and the columns each show being unbound, not sure if that is part of the issue.

Here is the code from the button

$button1_Click={
#TODO: Place custom script here
$row = $datagridview1.SelectedRows
Write-Host $row.Cells['Username'].Value
Write-Host $row.Cells['Column1'].Value
Write-Host $row.Cells['Column2'].Value
Write-Host $row.Cells['Column3'].Value
Write-Host $row.Cells['Column4'].Value
Write-Host $row.Cells['Column5'].Value
Write-Host $row.Cells['Column6'].Value
Write-Host $row.Cells['Column7'].Value
foreach ($cell in $row.Cells)
{
$cell.value
}
}

When I run the script and click the button, I get white space, here is what is in output when when I close the test run

>> Running (GetVDISessions.psf) Script...
>> Building (GetVDISessions.psf) ...
>> Platform: V5 64Bit (STA) (Forced)









*** PowerShell Script finished. ***
>> Execution time: 00:00:19
>> Script Ended

Note multiple blank lines they come up when the button is clicked that should be writing the values of the columns

Also attached an image of the Columns via the designer.

Could it be anything with the format or type of data that is in the Columns that is making it not read right

Thanks again
Attachments
DesignColumns.PNG
DesignColumns.PNG (25.52 KiB) Viewed 4542 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: datagridview cell reading

Post by jvierra »

You must set both the header text AND the name. THe column names are in the right box.

$row.Cells['DMS Name'].Value

SelectedRows can be zero or more rows.

It appears that you may not be loading the table correctly. THe table loads from a collection by "Data{PropertyName" when it matches the collections field name.
User avatar
FSU-Systems
Posts: 8
Last visit: Wed Oct 27, 2021 7:53 am

Re: datagridview cell reading

Post by FSU-Systems »

Think that may be what is not setting right, in the Designer I am setting both, via attached screenshot

Column Name = User
Header Text = Username

And with the Code

$button1_Click={
#TODO: Place custom script here
$row = $datagridview1.SelectedRows
Write-Host $row.Cells['User'].Value
Write-Host $row.Cells['Column2'].Value
Write-Host $row.Cells['Column3'].Value
Write-Host $row.Cells['Column4'].Value
Write-Host $row.Cells['Column5'].Value
Write-Host $row.Cells['Column6'].Value
Write-Host $row.Cells['Column7'].Value
#foreach ($cell in $row.Cells)
#{
#$cell.value
#}
}

I get white space is all, only updated the first one to the new name, after deleting and recreating all the columns

I am not sure, why on the right side, I do not see the Column Name I gave it anywhere in there just the Header name, although a Column name was specified.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: datagridview cell reading

Post by jvierra »

The column names are not what you are using. They are what you named them as when you created them.
This topic is 6 years and 8 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