How can I keep my buttercream from becomming grainy?
This can have different reasons.

How long did you mix it? The transition between grainy and creamy can come late and suddenly, but it should happen eventually.

Was the butter cold? I have been getting the best results with room-temperature butter.

Did you sieve the powdered sugar? If not, that may be the problem. Or did it perhaps get wet at some point and crystallize again?

If all these are NOT the problem, try adding some milk, or if you plan to use it anyway, vanilla essence, in small drops, while stirring.

I hope one of these will help :).