It takes one line.
$datagridview1.SelectedItems | ForEach-Object{$_.Selected = $false}
Datagridview MultiSelect without shift or CTRL
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.
- swindmiller
- Posts: 64
- Last visit: Tue Aug 22, 2023 11:59 am
Re: Datagridview MultiSelect without shift or CTRL
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:
should be this?
SelectedItems did not seem to work.
SelectedCells works also but still has the same issue as above.
Scott
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}
Code: Select all
$datagridview1.SelectedRows | ForEach-Object{$_.Selected = $false}
SelectedCells works also but still has the same issue as above.
Scott
Re: Datagridview MultiSelect without shift or CTRL
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
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
- swindmiller
- Posts: 64
- Last visit: Tue Aug 22, 2023 11:59 am
Re: Datagridview MultiSelect without shift or CTRL
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 ) 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:
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
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 ) 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}
}
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