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
datagridview event firing
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.
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.
- Alexander Riedel
- Posts: 8488
- Last visit: Tue Apr 16, 2024 8:42 am
- Been upvoted: 37 times
Re: datagridview event firing
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
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
Re: datagridview event firing
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
}
Re: datagridview event firing
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
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
Re: datagridview event firing
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
$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
Re: datagridview event firing
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.
Re: datagridview event firing
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)
}
}
if ($members -ne $null)
{
foreach ($member in $members)
{
$dgvMembers.Rows.Add($false, ($member.GivenName + " " + $member.Surname), $member.samAccountName)
}
}