Trying to change values based on Datagridview checkbox

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 4 days 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
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Trying to change values based on Datagridview checkbox

Post by supportMIB »

Product, version and build: 2017
Operating system: win10
PowerShell version(s): 5+
32-bit version of software?

I have a datagridview populated with data and I have several checkboxes in it. I would like to add a condition 'if checkbox is false' or true' but cannot seem to figure it out...The code I have its below:

When I check or uncheck the box it always returns 'false' when clicking an unchecked box.

Code: Select all

$datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
	#TODO: Place custom script here
	
	#col index
	#6 = cover
	$checkboxVal = $($datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value)
	if ($_.ColumnIndex -eq 6)
	{
		if ($checkboxVal -eq $false)
		{
			#do something here
			[System.Windows.Forms.MessageBox]::show($checkboxVal)
		}
		else
		{
			#do something else here
			[System.Windows.Forms.MessageBox]::show($checkboxVal)
		}
	}
}
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Trying to change values based on Datagridview checkbox

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Trying to change values based on Datagridview checkbox

Post by jvierra »

The "Value" property is a number or string set by the definition of the column. Set the value to return a Boolean then y9ou can test it directly. There is no need to reassign it. Creating amny new variables leads to hard to find errors, makes code unreadable and makes debugging harder.

This is how to find and test a Boolean value.
  1. $datagridview1_CellContentClick = [System.Windows.Forms.DataGridViewCellEventHandler]{
  2.     #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  3.    
  4.     if ($_.ColumnIndex -eq 6) {
  5.         if ($datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value) {
  6.             [System.Windows.Forms.MessageBox]::show($datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value)
  7.         }
  8.         else {
  9.             [System.Windows.Forms.MessageBox]::show($datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value)
  10.         }
  11.     }
  12. }
Untitled.png
Untitled.png (24.72 KiB) Viewed 1015 times
This topic is 2 years and 4 days 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