Datagridview selected cells

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 5 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 selected cells

Post by Shelltastic »

I am trying to capture the selected cells in a datagridview, using a multi-select datagrid. So far I have something like this, although not working..

$selections = $datagridview1.SelectedCells()
foreach ($item in $selections)
{
Do stuff
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview selected cells

Post by jvierra »

This depends on the configuration of the grid.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview selected cells

Post by jvierra »

You need to use the property as there is no method with that name. Look closely at the documentation.

foreach($item in $datagridview1.SelectedCells){
Do stuff
}


There is no "SelectedCells()"
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Datagridview selected cells

Post by Shelltastic »

Ok, that much I figured out when the code I tried didn't work.. So I am aware of that much up to this point. When the program is run, and the datagrid populates the data I am having fed to it from a powershell query, a user will be able to click on those results.. Screen shot below..

https://1drv.ms/u/s!AgBAbImA7NSlsxyQnBdWjslyeHA8

My question is, when a user clicks on a cell, I want to take the value of the cell or cell(s) clicked and do something with it. That is where I am struggling.
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Datagridview selected cells

Post by Shelltastic »

Disregard please. I was able to get the selected cell value by using $datagridview1.SelectedCells[0].Value

Thank you for the response.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview selected cells

Post by jvierra »

What is the issue.? The code enumerates all cells selected.
To access a cell on a click use the "CellClick" or "CellContentClick" event. The event object contains the cell column and row indexes.
This topic is 5 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