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.
pjbuckley
Posts: 31
Joined: Tue Nov 05, 2019 3:44 pm

Check all checkboxes not from column header

Post by pjbuckley » Fri Dec 06, 2019 3:58 pm

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: 14068
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Check all checkboxes not from column header

Post by jvierra » Fri Dec 06, 2019 4:04 pm

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
Joined: Tue Nov 05, 2019 3:44 pm

Re: Check all checkboxes not from column header

Post by pjbuckley » Fri Dec 06, 2019 4:10 pm

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: 14068
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Check all checkboxes not from column header

Post by jvierra » Fri Dec 06, 2019 4:16 pm

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
Joined: Tue Nov 05, 2019 3:44 pm

Re: Check all checkboxes not from column header

Post by pjbuckley » Mon Dec 09, 2019 9:26 am

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
Joined: Tue Nov 05, 2019 3:44 pm

Re: Check all checkboxes not from column header

Post by pjbuckley » Mon Dec 09, 2019 9:37 am

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: 14068
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Check all checkboxes not from column header

Post by jvierra » Mon Dec 09, 2019 1:45 pm

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
Joined: Tue Nov 05, 2019 3:44 pm

Re: Check all checkboxes not from column header

Post by pjbuckley » Mon Dec 09, 2019 2:04 pm

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: 14068
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Check all checkboxes not from column header

Post by jvierra » Mon Dec 09, 2019 3:27 pm

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
Joined: Tue Nov 05, 2019 3:44 pm

Re: Check all checkboxes not from column header

Post by pjbuckley » Mon Dec 09, 2019 3:56 pm

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.

Locked