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.
User avatar
PS_Ross
Posts: 65
Joined: Thu Mar 01, 2018 7:46 am

Datagridview Selection

Post by PS_Ross »

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: 7529
Joined: Tue May 29, 2007 4:43 pm
Answers: 1
Been upvoted: 3 times

Re: Datagridview Selection

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.

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

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
PS_Ross
Posts: 65
Joined: Thu Mar 01, 2018 7:46 am

Re: Datagridview Selection

Post by PS_Ross »

Am I missing something? Using the following...

$DisplayName = $datagridview1.SelectedCells[0].Value
$Database = $datagridview1.Row[$DisplayName.RowIndex].Cell[$DisplayName.CellIndex + 1]

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

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
PS_Ross
Posts: 65
Joined: Thu Mar 01, 2018 7:46 am

Re: Datagridview Selection

Post by PS_Ross »

Ah, ok missed that. I was still using it's value. Thanks bud.

User avatar
PS_Ross
Posts: 65
Joined: Thu Mar 01, 2018 7:46 am

Re: Datagridview Selection

Post by PS_Ross »

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 1035 times
1.PNG
1.PNG (4.73 KiB) Viewed 1035 times

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

Re: Datagridview Selection

Post by jvierra »

The code gets the adjacent cell. You need to extract the value.

User avatar
PS_Ross
Posts: 65
Joined: Thu Mar 01, 2018 7:46 am

Re: Datagridview Selection

Post by PS_Ross »

It doesn't even appear to get the adjacent cell. I get that error when I run the code you suggested verbatim.

User avatar
PS_Ross
Posts: 65
Joined: Thu Mar 01, 2018 7:46 am

Re: Datagridview Selection

Post by PS_Ross »

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



Locked