Greetings,
I have a boolean column in my DGV. I also have a button that first determines which rows have 'checked' boxes like so:
$checkedrows = $dgv.rows | where ($_.cells[0].value).
What I noticed was that I selected three rows but the above code only returned two.
Then I found this: 'Changes to underlying data source are applied when the controls lose the focus'
So what was happening was when I checked the third checkbox, i left the focus on that cell and pressed the button so the checked state was not being picked up.
What's the best way to make sure a checkbox value is correct even if the last cell 'check' is still in focus please?
Thanks
David
datagridview - ensure checkbox value is correct
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.
Re: datagridview - ensure checkbox value is correct
Found it: viewtopic.php?t=9730
I have to use the CellContentClick event to commit the edit.
I have to use the CellContentClick event to commit the edit.
Re: datagridview - ensure checkbox value is correct
I see no need to do this or to look at it this way. Just check the box and it will be checked. If you set it in data then you need to move the current row to a new row.
The real way to change this without all of these issues is to change the field in the underlying table. This sill be affective immediately.
You seem to be mixing up someone's instructions for how to manage a grid that does not have a DataTable attached. When we have a DataSource then we change teh DataSource to change the grid.
The real way to change this without all of these issues is to change the field in the underlying table. This sill be affective immediately.
You seem to be mixing up someone's instructions for how to manage a grid that does not have a DataTable attached. When we have a DataSource then we change teh DataSource to change the grid.
Re: datagridview - ensure checkbox value is correct
You keep inventing things that are not intended to be used the way you are using them. It will only get you in trouble later.
Why is it that you cannot click a checkbox? Are you trying to check it with a mouse or in code? That is not clear.
Why is it that you cannot click a checkbox? Are you trying to check it with a mouse or in code? That is not clear.
Re: datagridview - ensure checkbox value is correct
I have a datagridview with two columns - one a boolean displaying a checkbox and the second an AD account name.
I can select the checkbox with a mouse click.
That checkbox value is not $true until I shift the focus away from that cell. I need it to be true immediately without shifting focus.
That's why I use the event to make sure its true.
It the standard sort of thing that one codes for - select some stuff then press a button to do something to that selected stuff.
I can select the checkbox with a mouse click.
That checkbox value is not $true until I shift the focus away from that cell. I need it to be true immediately without shifting focus.
That's why I use the event to make sure its true.
It the standard sort of thing that one codes for - select some stuff then press a button to do something to that selected stuff.
Re: datagridview - ensure checkbox value is correct
Still don't understand. When checked it is true. Why w9ould you think it isn't.
When editing a control on the screen the value of the control is current. If you are using a DataTable then the table is placed in edit mode when you enter the cell. IF you move to any other cell or control the edit for the cell is posted to the underlying field. in any background event the value will not appear to be valid as long as the cell has the focus.
When editing a control on the screen the value of the control is current. If you are using a DataTable then the table is placed in edit mode when you enter the cell. IF you move to any other cell or control the edit for the cell is posted to the underlying field. in any background event the value will not appear to be valid as long as the cell has the focus.
Re: datagridview - ensure checkbox value is correct
If I use the mouse to check the box and then press a button which executes:
$checkedrows = $datagridviewSource.Rows | where { $_.Cells['Select'].Value }
..then no rows are returned. The box is clearly checked but the value of the cell is $false.
That is not a good user experience.
All I need to do is add this event:
... and the value will be $true so the 'button' code will work.
Of course I could check the box and then click somewhere else in the DGV to commit the edit but that also is not a good user experience.
$checkedrows = $datagridviewSource.Rows | where { $_.Cells['Select'].Value }
..then no rows are returned. The box is clearly checked but the value of the cell is $false.
That is not a good user experience.
All I need to do is add this event:
- $datagridviewSource_CellContentClick = [System.Windows.Forms.DataGridViewCellEventHandler]{
- #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
- if ($datagridviewSource.iscurrentcelldirty)
- {
- $datagridviewSource.CommitEdit([System.Windows.Forms.DataGridViewDataErrorContexts]::Commit)
- }
- }
Of course I could check the box and then click somewhere else in the DGV to commit the edit but that also is not a good user experience.
Re: datagridview - ensure checkbox value is correct
Don't know where you found that but it is surely the longest way around the barn.
Why not just test the "Checked" value of the cell? It will always be set even if the table hasn't been updated.
Anyway - I will let you invent things however you like. I still say learning the basics of forms and controls would save you a lot of time and frustration inventing things that are unnecessary.
Maybe tomorrow I will look into a demo of how to work with checkboxes in a DGV. The method doesn't matter if it is tied to a table or not.
Good luck.
Why not just test the "Checked" value of the cell? It will always be set even if the table hasn't been updated.
Anyway - I will let you invent things however you like. I still say learning the basics of forms and controls would save you a lot of time and frustration inventing things that are unnecessary.
Maybe tomorrow I will look into a demo of how to work with checkboxes in a DGV. The method doesn't matter if it is tied to a table or not.
Good luck.
Re: datagridview - ensure checkbox value is correct
What the button does is to actually remove the selected rows from the dgv.
Thats also fun as one cannot loop through a datatable collection to remove them.
Thats also fun as one cannot loop through a datatable collection to remove them.
Re: datagridview - ensure checkbox value is correct
Why would you think that. I can code a DGV to remove rows by looping through. With a table we just delete in place against a view that does not display deleted rows.
We can also use a checkbox to cause a row to be deleted.
It sounds to me like you are trying to use a DGV where we would use a CheckedListBox. This is why I keep repeating that learning forms correctly would make things much easier.