The evolution of a random and initially homogeneous distribution of parallel and infinitely extended edge dislocations is studied by using elastic energy minimization without and in presence of a periodic external stress, τa. During the energy minimization without external stress (relaxation), randomly distributed dislocation dipoles are formed whereas, when the external stress is acting, the dislocations condense in walls. We investigated the spatial periodicity of this microstructure, λ, as a function of, τa, and of the total dislocation density. The elastic energy of the stress-induced microstructure is found to be comparable to the value obtained by relaxation. Thereby, emphasis is given to the dynamical character of patterning. A phenomenological model has been developed, explaining the correlation between λ and τa found in the simulations and comparing favorably with existing experimental data.