Reputation: 2825
I was working on a c# project using a DataGridView and ran into an issue.
Basically, I have a grid that gets updated from any number of places and has a button column.
I want to capture button clicks (of course) and do something which involves the other cells from that button's row. As far as I know, the only way to associate a click with a row of the grid is via the RowIndex of the EventArgs.
What I'm worried about is that the grid may change between the user clicking and the event being delivered, causing an incorrect row to look like it was clicked.
Can this happen or am I being paranoid? If possible, is there anyway I can bind a reference to either the button or the row inside the eventargs so that I can differentiate the originating row even if its index has since changed?
Upvotes: 0
Views: 210
Reputation: 14591
GUI controls in WinForms are 'tied' to the UI thread.
When you click the row, grid implementation handles this and as a part of the click handling raises an event - all on the same UI thread. Even if other threads change the grid content, this has to be serialized to the UI thread (typically with posting Windows messages in the background, or BeginInvoke
in WinForms terminology - not exactly the same, but close).
This means that even if there are concurrent changes, if your click is already registered, the event will not be interrupted by UI update.
Note, however, that background thread could change the data object to which the row is bound while your handler runs, or between click occurred and event was handled. Still, it makes no difference to you, as grid UI update would need to be serialized and would still happen after your event handler.
Upvotes: 1