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.
User avatar
swindmiller
Posts: 64
Last visit: Fri Jan 21, 2022 4:36 am

Datagridview MultiSelect without shift or CTRL

Post by swindmiller »

Looking to be able to MultiSelect a datdgridview without using shift or CTRL. I am running this GUI on a touchscreen and don't want to have to use a keyboard (or onscreen keyboard) if possible. Is this possible?

Thanks,
Scott
jvierra
Posts: 15046
Last visit: Sat Jan 22, 2022 2:23 pm
Answers: 16
Has voted: 3 times
Been upvoted: 13 times

Re: Datagridview MultiSelect without shift or CTRL

Post by jvierra »

See this for variations on how to accomplish this.

https://stackoverflow.com/questions/417 ... dview?rq=1
User avatar
swindmiller
Posts: 64
Last visit: Fri Jan 21, 2022 4:36 am

Re: Datagridview MultiSelect without shift or CTRL

Post by swindmiller »

Thanks for the link.

I tried following along and am having trouble understanding. Would you happen to have an example in PowerShell Studio I could try to deconstruct to learn?
If you don't have time, its no big deal.

Thanks,
Scott
jvierra
Posts: 15046
Last visit: Sat Jan 22, 2022 2:23 pm
Answers: 16
Has voted: 3 times
Been upvoted: 13 times

Re: Datagridview MultiSelect without shift or CTRL

Post by jvierra »

According to Microsoft the on-screen keyboard is the correct way to use the shift key to multi-select.

Since your request is a non-standard use, I don't have a sample. Just remember the current selection in a script scoped variable and add teh new selection and reset the selected items on each selection. That should take about 5 lines of code. The issue is deciding how to go into multi-select mode. A context menu can do that.

Believe me. The onscreen keyboard is more natural for users and follows the Windows GUI spec and documentation for tablets. I always recommend not to re-inventing what the industry has already defined. Standards are less confusing to users and provide built-in help for users trained in Windows.
jvierra
Posts: 15046
Last visit: Sat Jan 22, 2022 2:23 pm
Answers: 16
Has voted: 3 times
Been upvoted: 13 times

Re: Datagridview MultiSelect without shift or CTRL

Post by jvierra »

Here is a quick and dirty example of how to multiselect on a row select.
Attachments
Test-DGVButton.psf
(12.79 KiB) Downloaded 6 times
User avatar
swindmiller
Posts: 64
Last visit: Fri Jan 21, 2022 4:36 am

Re: Datagridview MultiSelect without shift or CTRL

Post by swindmiller »

Thanks for the example, that helps a ton!!!

I will also reconsider just using the OSK, but this will help me learn this method anyway if I choose to go with the OSK.

Thanks,
Scott
User avatar
swindmiller
Posts: 64
Last visit: Fri Jan 21, 2022 4:36 am

Re: Datagridview MultiSelect without shift or CTRL

Post by swindmiller »

jvierra, this worked perfectly. Thanks!

One more question. I was trying to add a button to clear the selection, in order to select different rows.
This did work:

Code: Select all

$button1_Click={
	Clear-Variable selected
	$datagridview1.ClearSelection()
}
but the "selections" seem to be remembered. For example if I select 3 rows, then hit the button I created above, it does unselect them (and clear all selected rows) but if I select another row it selects that one and the ones that were selected previously.

Sorry, I know this is probably simple but I just cant seem to get it to work.

EDIT: Here is the entire code if that helps.

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.ClearSelection()
}
Thanks,
Scott
jvierra
Posts: 15046
Last visit: Sat Jan 22, 2022 2:23 pm
Answers: 16
Has voted: 3 times
Been upvoted: 13 times

Re: Datagridview MultiSelect without shift or CTRL

Post by jvierra »

Get the selected items and clear them one-by-one. A multi-select mode cannot be cleared with a single "ClearSelection" which clears only the most current selection.
User avatar
swindmiller
Posts: 64
Last visit: Fri Jan 21, 2022 4:36 am

Re: Datagridview MultiSelect without shift or CTRL

Post by swindmiller »

Thanks, that makes sense.

I tried this as a test:

Code: Select all

$button1_Click= {
	Clear-Variable selected
	$datagridview1.Rows[2].Selected = $false
}
after making sure Row2 was selected and it does unselect the row but if I then select a different row, it selects that row plus the one that was unselected (Row2 in this case). It seems to be remembering what was selected.

Or am I going completely in the wrong direction :D
jvierra
Posts: 15046
Last visit: Sat Jan 22, 2022 2:23 pm
Answers: 16
Has voted: 3 times
Been upvoted: 13 times

Re: Datagridview MultiSelect without shift or CTRL

Post by jvierra »

Enumerate "SelectedItems" and set "Selected" to false.
Post Reply