The positioning/size calculations are done in GridCellComment.InitializeComment. At the bottom of that method, you can test to see if the panel will be off the display, and if so then move it.
Here is the sample modified to do this. The changes included adding the grid to the call list for the InitializeComment method so you could use its clientwidth. Also, a property was added to indicate whether the panel was moved so the mouse controller knows not to draw the little arrow. I am not sure what you want to do about drawing the arrow. I just did not draw it in this case. You could draw it in some other position if you want.