I have a databound grid with a templated field column that is a checkbox. When the user checks the checkbox, it will autopostback and update the row to indicate the box’s check state.
My first attempt was to simply use the OnCheckedChanged method, but when this gets called, I have no way to know which row the checkbox came from. Thus, I don’t know which row to update.
Can anyone suggest a method to determine which row the checkbox that fired the oncheckedchanged event came from? Or can you suggest a better way to achieve what I need to do?
I cannot bind the checkbox to the data column because I do not want the checkbox disabled in select mode, and do not wish to require the user to enter edit mode to change the value. I also need all rows to be editable. So, the best route seems to be in a templated column.
You could find the checked Checkbox by iterating over the rows.
This isn’t as inefficient as it looks like(when the Grid’s PageSize is not too high).
You can also get the GridViewRow via Checkbox.Parent.Parent:
This is faster as option 1 but if you f.e. would nest the Checkbox in a Table in the future, this wouldn’t work anymore without adjustment.
I would normally prefer option 1, because in an event-handler one millisecond more or less doesn’t matter.
Update: getting the GridViewRow via NamingContainer is another(in my opinion the best) option:
This even still works when you nest the Checkbox inside other Controls like a Table.