Get value of cell based on another cell from datagridview

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
User avatar
lontru
Posts: 83
Last visit: Mon Jan 17, 2022 6:08 am

Get value of cell based on another cell from datagridview

Post by lontru »

Image

I know how to get selectedvalue from area 1 with : $datagridviewResultsLogs.SelectedCells[0].Value
but how do i get the value in area 2 when selecting cell from area 1 - the same row.
jvierra
Posts: 15039
Last visit: Mon Jan 17, 2022 10:20 am
Answers: 16
Has voted: 3 times
Been upvoted: 13 times

Re: Get value of cell based on another cell from datagridview

Post by jvierra »

Use the column name and not the index. The index is transient and the name always specifies the correct column.

$datagridviewResultsLogs.SelectedCells['Device'].Value
User avatar
lontru
Posts: 83
Last visit: Mon Jan 17, 2022 6:08 am

Re: Get value of cell based on another cell from datagridview

Post by lontru »

Code: Select all

$SelectedCell = $datagridviewResultsLogs.SelectedCells['Device'].Value
$SelectedLogfilename = $datagridviewResultsLogs.Rows[$($datagridviewResultsLogs.CurrentCell.RowIndex)].Cells[1].Value
$SelectedDevice = $datagridviewResultsLogs.Rows[$($datagridviewResultsLogs.CurrentCell.RowIndex)].Cells[0].Value
$script:selectedlogfile = "\\$SelectedDevice\$logdir\$SelectedLogfilename"
would above be the way to solve it or is there a better way
Locked