All-optical switching and codirectional mode conversion in nonlinear gratings