Gramling was the court magician at Fluxford Castle. Marton Dhal was his former apprentice. Gramling's research revealed the location of Merlin's Helm. An expedition was sent to recover it, and Gramling volunteered to use it, even though he knew it would destroy his mind. This was in part to make up for not recognising Dhal's evil when he was an apprentice. Dhal was controlling Gramling's mind and using him as spy at the court.
However, the control was not as complete as he thought. While wearing the Helm, before it irreparably damaged him, Gramling switched bodies with Dhal, and thus died in Gramling's place. He took over control of Susan Foreman and Princess Mellisa, and planned to use them to operate the Helm until he changed the world to his desire. Before he could do so, the First Doctor, Anni Glassfeather and Kildenny Odoyle combined their magic powers and attacked. Gramling killed Odoyle, but the Doctor was able to destroy Gramling. (PROSE: The Sorcerer's Apprentice)