datagridview event firing

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 3 years and 7 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
User avatar
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

datagridview event firing

Post by IanUoY »

I have a datagridview with a checkbox column.
I've been unable to fire an event immediately after I've ticked the checkbox. eg CellClick etc obviously fire immediately, but I wish to execute some code after I've ticked the checkbox. How do I do this. Tried so many cell events and none seem to work
In the events, I check whether the checkbox has been ticked and then execute code which is dependent on whether the checkbox is true or not. But of course, the events are fired before. If I move to a new row then the event does fire, as it always does, but naturally supplies the details of the previous rows values.
Any help most appreciated, or should I use a button column?
Regards
Ian
User avatar
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

Re: datagridview event firing

Post by IanUoY »

Apologies, could a moderator move to the correct forum please?
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: datagridview event firing

Post by Alexander Riedel »

[Moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

Re: datagridview event firing

Post by IanUoY »

Sorry I should just clarify something.

It is when I click the checkbox event, that I get the correct information back. This is because the value is already true, even though after clicking it again, it remove the check.
I need someway of checking if the checkbox has changed...have tried state changed and made no difference.

Cheers

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

Re: datagridview event firing

Post by jvierra »

Here is the quick and simple answer to how to get the value of the checked item.

Code: Select all

$datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
	Write-Host $datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].EditedFormattedValue
}
User avatar
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

Re: datagridview event firing

Post by IanUoY »

I'm afraid that didn't work.
When I first click the datagridview checkbox, it fires the event, however it does not recognise that the checkbox is now ticked, presumably because the event fires immediately cell is clicked rather than after the tick is put there. In the code I check whether the checkbox is ticked, and for the purposes of testing, write the contents of the row to my status bar.
If I leave the checkbox ticked and then tick a different rows checkbox, and then go back to the original row, click the checkbox, then it displays the correct information. Presumably because when I click the cell, it still is ticked when event fires, so my code evaluates to true and displays the info. But of course, by now the checkbox has been unchecked, if you see what I mean
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: datagridview event firing

Post by jvierra »

Works every time for me. I suspect you have the grid set up incorrectly. The gid must have an underlying data source for events to work corretlly.
User avatar
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

Re: datagridview event firing

Post by IanUoY »

I populate the datagridview with AD members using following code:

$members = get-aduser -Filter { Surname -like $searchString } -SearchBase "OU=xxx,DC=xxx,DC=xxx,DC=xxx,DC=xxx" | Sort-Object GivenName, Surname

But quite simply the value of the status checkbox using on cellcontentclick is only picked up on second click (which effectively toggles it off).

My intention is to replace the checkbox column with a button column. I've tested this and this works for me.

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

Re: datagridview event firing

Post by jvierra »

Your method of getting the data does not work correctly with a DataGridView. Without the complete code there is really no way to understand the issue you are asking about. I can crate a grid that the checkbox events correctly on and teh code I posted works as expected. If you do not create and load the grid correctly then the checkbox will not event correctly.
User avatar
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

Re: datagridview event firing

Post by IanUoY »

Once I've executed the above code I populate the grid as below:
if ($members -ne $null)
{
foreach ($member in $members)
{
$dgvMembers.Rows.Add($false, ($member.GivenName + " " + $member.Surname), $member.samAccountName)
}
}
This topic is 3 years and 7 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