The story is vexing enough as it is, but it is worth looking at from other angles.
1. Consider that this story ends the 2nd book of Samuel, but it's simply somewhere in the midst of Chronicles.
2. Note that in Samuel, David simply gets an idea to conduct a census, something that does not seem to cause any displeasure anywhere else. (e.g., birthdays according to Bethel are pagan, but not the census every decade - and no objections raised about Joseph and Mary reporting to Bethlehem). Consequence: an ultimatum delivered from on high via a prophet named Gad. Pick your punishment,
3. The story in its second telling (Chronicles) says nothing about the events preceding it in Samuel ( David and Baathsheba).
4. In Chronicles, David got the idea from Satan...
Other than in Job, as far as I know, this is the only mention of "Satan" in the Old Testament. And he advises David to conduct a census of Judah and Israel... Can you imagine anything more sinister in the eyes of God?
Rhetorical question, of course. But look what happens as a result. David selects a site for an offering in Jerusalem to come to terms with the Lord in penance and the First Temple is erected on the site.
I see in this story a garbled, legendary explanation for why the Temple was built where it was. Re-told in Chronicles, David was a darling and not the ambiguous figure he was to the scribes of Samuel ( and Kings). David's capital has increased.
At about that time too, pretenders to the throne in the Persian protectorate of Judah were capitalizing on the lineage. But it didn't work. We've got Nehemiah and Ezra giving accounts of the early days - and then Judah appears to enter a phase of relative prosperity and happiness under a couple of centuries of Persian kings. They don't march off into exile so much as spread out within the Empire to Asia Minor and Mesopotamia...
By all indications Chronicles was written shortly after the time when its narrative concluded, with the return from Exile and a lot of influences of the Persian sponsors of the return. The Persians were emerging as a Zoroastrian nation; if not Cyrus ostensibly, then certainly his successors. They were dualistic and spoke of many things that earlier Hebrew texts did not.