The atomic thickness of two-dimensional materials provides a unique opportunity to control their electrical1 and optical2 properties as well as to drive the electronic phase transitions3 by electrostatic doping. The discovery of two-dimensional magnetic materials4-10 has opened up the prospect of the electrical control of magnetism and the realization of new functional devices11. A recent experiment based on the linear magneto-electric effect has demonstrated control of the magnetic order in bilayer CrI3 by electric fields12. However, this approach is limited to non-centrosymmetric materials11,13-16 magnetically biased near the antiferromagnet-ferromagnet transition. Here, we demonstrate control of the magnetic properties of both monolayer and bilayer CrI3 by electrostatic doping using CrI3-graphene vertical heterostructures. In monolayer CrI3, doping significantly modifies the saturation magnetization, coercive force and Curie temperature, showing strengthened/weakened magnetic order with hole/electron doping. Remarkably, in bilayer CrI3, the electron doping above ~2.5 × 1013 cm-2 induces a transition from an antiferromagnetic to a ferromagnetic ground state in the absence of a magnetic field. The result reveals a strongly doping-dependent interlayer exchange coupling, which enables robust switching of magnetization in bilayer CrI3 by small gate voltages.