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
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Datagridview Selection

Post by Shelltastic »

Looking for a way to grab the cell adjacent to the user selection in my Datagridview.

I am using "$Selection = $datagridview1.SelectedCells[0].Value" to get the selected cell, but not sure what I can use to grab the cell next to it also, as I will need both values.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Datagridview Selection

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
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 »

Selected Cell has a row index. Just get teh row and cell by incrementing the indexes.

$adjacent = $datagridview.Row[$cell.RowIndex].Cell[$cell.CellIndex + 1]
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Datagridview Selection

Post by Shelltastic »

Am I missing something? Using the following...

$DisplayName = $datagridview1.SelectedCells[0].Value
$Database = $datagridview1.Row[$DisplayName.RowIndex].Cell[$DisplayName.CellIndex + 1]
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 »

You need a cell and not its value.


$cell = $datagridview1.SelectedCells[0]
$Database = $datagridview1.Row[$cell.RowIndex].Cell[$cell.CellIndex + 1]
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Datagridview Selection

Post by Shelltastic »

Ah, ok missed that. I was still using it's value. Thanks bud.
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Datagridview Selection

Post by Shelltastic »

Looks like I am still failing. I need to get the adjacent cells value, not just index, looks like I didn't mention that in my original post. Attachments included.
Attachments
2.PNG
2.PNG (17.19 KiB) Viewed 2359 times
1.PNG
1.PNG (4.73 KiB) Viewed 2359 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 »

The code gets the adjacent cell. You need to extract the value.
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Datagridview Selection

Post by Shelltastic »

It doesn't even appear to get the adjacent cell. I get that error when I run the code you suggested verbatim.
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Datagridview Selection

Post by Shelltastic »

Disregard, I was able to figure it out. I just manually get the RowIndex and grab the next cell over from the selection, kept it simple. For reference I am using the code below, thanks for the assistance.

Code: Select all

	$cell = $datagridview1.SelectedCells[0]
	$RowIndex = $cell.RowIndex
	$adjacent = $datagridview1.Rows[$RowIndex].Cells[1].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