Datagridview MultiSelect without shift or CTRL

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 2 years and 1 month 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview MultiSelect without shift or CTRL

Post by jvierra »

It takes one line.

$datagridview1.SelectedItems | ForEach-Object{$_.Selected = $false}
User avatar
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Re: Datagridview MultiSelect without shift or CTRL

Post by swindmiller »

Thank you, yes, that does work but still does what I had an issue with before. If I select 3 rows (which it highlights/selects), then hit the button with your code, it does deselect them. But then if I select a row again it selects that row plus the other 3 I had selected (and deselected) prior. That seems to be my main hang up.

Thanks for sticking with me on this, I really appreciate your help!

EDIT: also I assume this line:

Code: Select all

$datagridview1.SelectedItems | ForEach-Object{$_.Selected = $false}
should be this?

Code: Select all

$datagridview1.SelectedRows | ForEach-Object{$_.Selected = $false}
SelectedItems did not seem to work.
SelectedCells works also but still has the same issue as above.

Scott
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview MultiSelect without shift or CTRL

Post by jvierra »

You obviously have code somewhere that is confusing you I recommend learning PowerShell and learn how WinForms is int3ended to be used. Without some technical training in coding WinForms, you will just be going in circles by guessing. WinForms and any complex technical system cannot be learned by guessing or copying code from the Internet.

WinForms is an object system that is event driven. You need to understand how the objects are intended to be used and how to use the persisted state of controls. Variables are rarely needed in forms and should be avoided. A "WinForm" is a state machine that persists state and is state changed through event code. Old simple "Basic" style programming won't work.

Here is an excellent fr4ee downloadable book that is an excellent way to learn PowerShell correctly.

https://www.sapien.com/books_training/W ... werShell-4
User avatar
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Re: Datagridview MultiSelect without shift or CTRL

Post by swindmiller »

Thanks, I will try to see what I can find.

The only reason I asked here is because its the code you provided to me that I am testing this with. I thought the same thing, that it was something I had set wrong (as it usually is :D ) so I wanted to eliminate me as making an error. I have added the code to the Test-DGVButton.psf that you attached earlier and that is the result.

This is the entire script I am running, the only thing I changed (added in this case) was the Button1_Click event:

Code: Select all

$form1_Load={
}

$buttonLoad_Click={
    $nac = Get-WmiObject -ClassName Win32_NetworkAdapterConfiguration #-ComputerName $ComputerName.Text
    $datagridview1.DataSource = [System.Collections.ArrayList]$nac
}

$datagridview1_SelectionChanged={
    if($datagridview1.CurrentRow.Index -ge 0){
        if($datagridview1.CurrentRow.Index -notin $selected){
            [array]$script:selected += $datagridview1.CurrentRow.Index
        }
        $selected |
            ForEach-Object{
                $datagridview1.Rows[$_].Selected = $true
            }
    }
}

$button1_Click= {
	Clear-Variable selected
	$datagridview1.SelectedCells | ForEach-Object {$_.Selected = $false}
}
Also attaching the PSF, this is the only thing I am running.
Do you have the same result when running or does it work properly for you? Just trying to figure out where I would have something set elsewhere.

Thanks,
Scott
Attachments
Test-DGVButton.psf
(14.95 KiB) Downloaded 74 times
This topic is 2 years and 1 month 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