Page 1 of 2

Datagridview Selection

Posted: Fri Apr 17, 2020 11:58 am
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.

Re: Datagridview Selection

Posted: Fri Apr 17, 2020 12:25 pm
by Alexander Riedel
[Topic moved by moderator]

Re: Datagridview Selection

Posted: Fri Apr 17, 2020 12:36 pm
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]

Re: Datagridview Selection

Posted: Fri Apr 17, 2020 1:40 pm
by PS_Ross
Am I missing something? Using the following...

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

Re: Datagridview Selection

Posted: Fri Apr 17, 2020 1:48 pm
by jvierra
You need a cell and not its value.


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

Re: Datagridview Selection

Posted: Fri Apr 17, 2020 1:50 pm
by PS_Ross
Ah, ok missed that. I was still using it's value. Thanks bud.

Re: Datagridview Selection

Posted: Fri Apr 17, 2020 2:05 pm
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.

Re: Datagridview Selection

Posted: Fri Apr 17, 2020 3:59 pm
by jvierra
The code gets the adjacent cell. You need to extract the value.

Re: Datagridview Selection

Posted: Mon Apr 20, 2020 5:03 am
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.

Re: Datagridview Selection

Posted: Mon Apr 20, 2020 6:04 am
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