Alrighty, I am having a tough time narrowing this down. I have a simple grid...2 columns...1 with a checkbox, the other is a readonly string.
My goal is to update the UI whenever the checkbox is checked or unchecked, but it isn't working the way I expect.
If I use the AfterCellUpdate event, then nothing gets fired till the user clicks off of the row. That is too confusing.
If I use the CellChange event, then I can't seem to reliably find out whether the user just checked or unchecked the item.
If I use the MouseClick event then all sorts of screwy stuff starts happening.
What event is closest to the standard CheckChanged event on a normal checkbox? I wouldn't think this would be all that tricky...
David
Hi,
The proper event to use is CellChange. You need to use the Text property of the cell to determine the current value of the checkbox. Value will not have ben updated, yet.
At first glance, this probably does not make a lot of sense. And for a checkbox cell, it really doesn't. But to understand why this is the case, think about a DateTime cell.
Suppose I have a grid with a DataTime cell and I want to change the date to "12/25/07". I would click on the cell and delete the contents and then type in some numbers. But it would not make sense for the grid to update the cell on every keystroke. The first key I typed would be a number "1", and this would not be a valid date. So what happens is that while the cell is in edit mode, the Value of the cell (which returns a DateTime) may not be valid if it's based on what I am typing. So the Value property of the cell does not look at what I type as I type it, it reads from the underying data source. To get what's actually typed into the cell at any given moment, I need to check the Text property. This is always okay, because anything I type can be represented as a string. It is only when the cell loses focus that the grid figures the user is done typing and tries to take the text you typed in and convert it to a valid date.
The checkbox cell would not have this problem, of course, since the user can't type anything invalid into it, but the grid still has to work the same way. So when you click the checkbox, only the Text property is updated, and the Value does not get updated until you leave the cell.
I am having the same issue with this checkbox event. My problem is that i need to update a monetary total on the grid caption whenever a row is checked or unchecked. I can account for the value not being updated yet, that is not a problem.
The problem is when you check or uncheck the same cell more than once the value of the cell does not change in the event. I supposed the issue at stake is the behavior you described with the datetime, but nevertheless i have to find a workaround.
How should i handle this? I need the total to update when the checkbox is changed, not when it loses focus.
Ah...my bad must have missed that. Works. Thanks!
Hi Jason,
As I explained above, what you do is handle the CellChange event and examine the cell's Text property to determine the currently value. There's really nothing to it.