When making cheese sauce, it is important to cook the sauce at a low temperature.  If the cheese sauce is overheated, the proteins can become denatured and the cheese may curdle.  This will result in gritty or grainy sauce. Ideally, you should add cheese last once all the other ingredients are mixed together and heated.  Turn off the heat and stir the cheese into the hot pot.  That should be sufficient heat to melt it.