Grainy frosting can happens when the frosting is over-whipped. Make sure to stop whipping as soon as the mixture achieved stiff peaks, or even slightly before.