The modal method based on Gegenbauer polynomials (MMGE) is extended to the case of bidimensional binary gratings. A new concept of modified polynomials is introduced in order to take into account boundary conditions and also to make the method more flexible in use. In the previous versions of MMGE, an undersized matrix relation is obtained by solving Maxwell's equations, and the boundary conditions complement this undersized system. In the current work, contrary to this previous version of the MMGE, boundary conditions are incorporated into the definition of a new basis of polynomial functions, which are adapted to the boundary value problem of interest. Results are successfully compared for both metallic and dielectric structures to those obtained from the modal method based on Fourier expansion (MMFE) and MMFE with adaptative spatial resolution.