So I was hung up last week on a data validation issue in WinForms. It’s a pretty simple desktop application to which I was adding a new Form for maintaining a parent-child-grandchild set of tables. DataGridView is my friend!
One column in the parent table was best represented by a set of radio buttons set to the side of the DataGridView. Turns out, there’s not a super-simple way to bind a radio button group to a column in a DataGridView.
I figured the easiest thing would be to populate the radio buttons from the corresponding column of the DataGridView when the user goes into a record, and to stuff the selected radio button value back into the column when the user moves away from the record. Do-it-yourself binding, pretty much.
Turns out, the Enter event works just great for the former. When I enter a record, the debugger shows the current value of my column in the underlying data source. Yay.
The latter is trickier. I tried using the Leave event, but that’s too late; my current row has already been shifted. I tried using the Validating event, but it didn’t pick up on the changed value of the radio button group either.
A moment with Google gave the answer: radio buttons within group boxes catch the Validating event of the containing box, not the of the whole form. Once I added a Validating event handler to the group box, my radio button values were picked up as current and properly saved.
There’s probably a really good reason for .NET to do this. sigh