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 1 year and 2 weeks 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
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

DataGridView & checkbox

Post by Domtar »

hi,

I have a DataGridView that has 3 columns, first is a checkbox cell, other 2 are textbox cells.

how can I load the info in it correctly so that the checkbox will not be checked, but will return unchecked if i look at the value?

I do this but when I click on it the first time, it returns False

foreach (...) {
$datagridADGroups.Rows.Add($false, $group.name, $group.info)
}

thanks!
by jvierra » Fri Mar 10, 2023 2:28 pm
Edit the column object and set it to detect the value you want.

See:
Screenshot 2023-03-10 172614.png
Go to full post
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView & checkbox

Post by jvierra »

Edit the column object and set it to detect the value you want.

See:
Screenshot 2023-03-10 172614.png
Screenshot 2023-03-10 172614.png (42.94 KiB) Viewed 2673 times
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: DataGridView & checkbox

Post by Domtar »

so I set the values like you showed me (thanks for that by the way) but now when I test it, the code returns $False if I click the checkbox or not. I have no idea why it won't return $False or $True, only $False on both clicks. Like it is not seeing the checkbox is checked.

log-toStatusBox -color Green -text "selection is $($datagridADGroups.CurrentRow.Cells[0].Value)`n"

this is how i check the cell, am I doing it wrong?

thanks.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView & checkbox

Post by jvierra »

You have to address the exact cells value by row number and column name.

$datagridview1.Rows[0].Cells['column1'].Value
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: DataGridView & checkbox

Post by Domtar »

i have no idea why this won't work for me.

my latest try, where nothing is displayed, even though I did check a cell

Code: Select all

for ($i = 0; $i -lt $datagridADGroups.Rows.Count; $i++) {
	if ($datagridADGroups.rows[$i].Cells[0].Value -eq $true) {
		log-toStatusBox -color Green -text "index = $i`n" -skipDate $false
	}
}
I'm losing my mind over this, looks simple enough but I can't get it to work.

:-(
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: DataGridView & checkbox

Post by Domtar »

OK I was able to understand part of the issue.

I load the DataGridView with

Code: Select all

$datagridADGroups.Rows.Add($false, $group.name, $group.info)
looping thru it, all the values are $False and they do not chance to $True.

as a test, i tried this

Code: Select all

$datagridADGroups.Rows.Add($true, $group.name, $group.info)
now when I loop, all the values are $True, even the ones i uncheck.

how can I load the data in the datagrid so that it won't be static?

thanks for the help!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView & checkbox

Post by jvierra »

Look at the definitions for the "Row.Add" methods. What you are doing is not how to add a row.
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: DataGridView & checkbox

Post by Domtar »

can you please post an example?

looking at the definition PowerShell Studio shows me, I do not see anything wrong.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView & checkbox

Post by jvierra »

An example of what? You can create rows and set cell values or use an array to assign all cells.

If you right-click any control in the toolbox it will show you links to the documentation and example code.
This topic is 1 year and 2 weeks 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