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.
jvierra
Posts: 14437
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Re: Datagridview Selection

Post by PS_Ross »

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 108 times

jvierra
Posts: 14437
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Locked