Datagridview Selection

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

Re: Datagridview Selection

Post by jvierra »

That is because you didn't want the adjacent cell you wanted a specific cell.

Just change one value in my code and extract the value.

Code: Select all

$cell = $datagridview1.SelectedCells[0]
$Database = $datagridview1.Row[$cell.RowIndex].Cell[$cell.ColumnIndex + 1].Value
Also adjacent means either current column index + 1 or current column index -1

Posting pictures of code made it hard for me to read teh error and your code. THe issue is fixed here:
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Datagridview Selection

Post by Shelltastic »

Correction, I DO want the adjacent cell from the selected cell. See screen shot below. The blue cell is the selected cell, the red arrow points to the cell's value I also need to grab along with the selected cell, hence, I need the "adjacent" cell. In my scenario, the adjacent cell will always be to the right of the selected cell due to the way the datagrid loads, so the code I posted above gets me the desired result every time.

Once again, thank you for the assistance.
Attachments
Capture.PNG
Capture.PNG (28.67 KiB) Viewed 818 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 Selection

Post by jvierra »

"Adjacent" can be either left or right of the current cell.

To get a specific cell use its name.

$Database = $datagridview1.Row[$cell.RowIndex].Cell['ServerName'].Value
This topic is 3 years and 11 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