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.
Datagridview Selection
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.
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.
- Alexander Riedel
- Posts: 7563
- Joined: Tue May 29, 2007 4:43 pm
- Been upvoted: 5 times
-
- Posts: 14674
- Joined: Tue May 22, 2007 9:57 am
- Has voted: 1 time
- Been upvoted: 5 times
- Contact:
Re: Datagridview Selection
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]
$adjacent = $datagridview.Row[$cell.RowIndex].Cell[$cell.CellIndex + 1]
Re: Datagridview Selection
Am I missing something? Using the following...
$DisplayName = $datagridview1.SelectedCells[0].Value
$Database = $datagridview1.Row[$DisplayName.RowIndex].Cell[$DisplayName.CellIndex + 1]
$DisplayName = $datagridview1.SelectedCells[0].Value
$Database = $datagridview1.Row[$DisplayName.RowIndex].Cell[$DisplayName.CellIndex + 1]
-
- Posts: 14674
- Joined: Tue May 22, 2007 9:57 am
- Has voted: 1 time
- Been upvoted: 5 times
- Contact:
Re: Datagridview Selection
You need a cell and not its value.
$cell = $datagridview1.SelectedCells[0]
$Database = $datagridview1.Row[$cell.RowIndex].Cell[$cell.CellIndex + 1]
$cell = $datagridview1.SelectedCells[0]
$Database = $datagridview1.Row[$cell.RowIndex].Cell[$cell.CellIndex + 1]
Re: Datagridview Selection
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 (17.19 KiB) Viewed 1091 times
-
- 1.PNG (4.73 KiB) Viewed 1091 times
Re: Datagridview Selection
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
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