Check all checkboxes not from column header

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 4 years and 3 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
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Check all checkboxes not from column header

Post by pjbuckley »

I am still having trouble creating a "SelectAll" checkbox column that actually selects all checkboxes in a ROW not a column. The "SelectALL" column is the third column in the table that i would like to check all the boxes after it in the same ROW. The column names that follow the "SelectALL" column are dynamically generated so the column names are unknown prior to table generation. Here is my code so far:

Code: Select all

$CheckAll_click = {
	for($i=0;$i -lt $DGV1.RowCount;$i++){
		if($DGV1.Rows[$i].Cells['SelectAll'].Value -eq $true) {
			for($j=3;$j -le $DGV1.ColumnCount;$j++){
				($DGV1.Rows[$i].Cells | ?{$_.ColumnIndex -eq $j}).Value=$true
			}
		}
		else {
			for($j=3;$j -le $DGV1.ColumnCount;$j++){
				($DGV1.Rows[$i].Cells | ?{$_.ColumnIndex -eq $j}).Value=$false
			}
		}
	}
} 
Thanks for the look!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Check all checkboxes not from column header

Post by jvierra »

You never said you want only the checkboxes in a row. That is a completely different beast. How do you plan to select the row and what action will select all checkboxes?
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Re: Check all checkboxes not from column header

Post by pjbuckley »

I have created a "SelectALL" checkbox column. I would like to check the box in the "SelectALL" column in the corresponding row that then selects all the checkboxes in that row.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Check all checkboxes not from column header

Post by jvierra »

That will not generate an event. You need to use a method that generates an event. The easiest way is to use a ContextMenu on the DGV.
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Re: Check all checkboxes not from column header

Post by pjbuckley »

That's odd. Removing one "for" loop out of that code will check all the boxes in the column. I dont really know how to build a context menu around this.
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Re: Check all checkboxes not from column header

Post by pjbuckley »

I keep seeing online how there is a property in a checkboxcell named : Checked or isChecked.

Where do i access this property. Is it on the row, column or attached to the data somehow?

Even knowing this im not sure it would get me the answer i need.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Check all checkboxes not from column header

Post by jvierra »

If something is defined for a cell then the property or method is on the cell.

This access the cells properties:
$DGV1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].<propertyname>

To access all cells in a row:
$DGV1.Rows[$_.RowIndex].Cells | ForEach-Object{ $_.Checked = $true}

To get the current row or selected rows use the appropriate properties of the DGV.

$dgv.CurrentRow
$dgv.SelectedRows


To display a context menu add the context menu to the DGV property - "ContextMenu".
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Re: Check all checkboxes not from column header

Post by pjbuckley »

This bit of code:

$DGV1.Rows[$_.RowIndex].Cells | ForEach-Object{ $_.Checked = $true}

Where does the $_.checked property exist when the column header names are unknown and the cells are dynamically generated as boolean?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Check all checkboxes not from column header

Post by jvierra »

Why are you asking about column headers? You are trying to check cells and not headers. This checks all of the cells in the target row.
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Re: Check all checkboxes not from column header

Post by pjbuckley »

OK. Where is the $_.checked property on the cell?

get-member does not return the $_.checked property on the row, column, or cell. Had it been there from my very first post, i wouldn't still be stuck on this.
This topic is 4 years and 3 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