Devarim 17.15 “you shall set a king over you, one whom the L-RD, your G-D, chooses….”
1 Chronicles 28.4 (And David said) “Howbeit the L-RD, the G-D of Israel, chose me out of all the house of my father to be king over Israel for ever; for He hath chosen Judah to be prince, and in the house of Judah, the house of my father, and among the sons of my father He took pleasure in me to make me king over all Israel;”
There is a difference between HaShem choosing a king and the people choosing a king: HaShem chose David as king because He found David “a man after the heart of HaShem” : 1 Samuel 13.14; 1 Samuel 16.1, 7 –
And the people chose (demanded) Saul (1 Samuel 8.19) to be king to judge them “like all the nations around them” because he was taller than all his fellows: 1 Samuel 9.2 –
“And he had a son, whose name was Saul, young and goodly, and there was not among the children of Israel a goodlier person than he: from his shoulders and upward he was higher than any of the people.”
“And the L-RD said unto Samuel: ‘How long wilt thou mourn for Saul, seeing I have rejected him from being king over Israel? fill thy horn with oil, and go, I will send thee to Jesse the Beth-lehemite; for I have provided Me a king among his sons.’ “
But the L-RD said unto Samuel: ‘Look not on his countenance, or on the height of his stature; because I have rejected him; for it is not as man seeth: for man looketh on the outward appearance, but the L-RD looketh on the heart.‘
Saul was rejected as king for numerous reasons but one in particular stands out: due to fear, “he forced himself to offer a sacrifice” 1 Samuel 13.12 from that which was under a cherem (a ban) as war booty of the Amalakites; (Navi Shemuel instructed Saul to utterly destroy all the Amalakites along with all their possessions;) offering a sacrifice is something that is a prerogative of the Kohanim.
Yet King David offered sacrifices (that he purchased 2 Samuel 24.24) which were accepted of HaShem, by fire coming down from heaven and consuming the sacrifices.
“And the king said unto Araunah: ‘Nay; but I will verily buy it of thee at a price; neither will I offer burnt-offerings unto the L-RD my G-D which cost me nothing.’ So David bought the threshing-floor and the oxen for fifty shekels of silver.”
And King David as we see was (like his forefather Ya’acov) a man after the heart of HaShem: Ya’acov said, ‘If God be with me … Then this shall be the House of God….’ “
” ‘If G-D is with me, and guards me on the path I take, and gives me food to eat and clothes to wear, and returns me safely to my father’s house, being my G-D, then this monumental stone shall be the house of G-D, and everything He gives me, I will tithe.’ “
As related in “A Song of David for the Dedication of the House” by Rav Yehuda Shaviv:
A song of ascents: G-D, remember for David all his affliction; how he swore to G-D and vowed to the mighty one of Ya’akov: “Surely I will not come into the tent of my house, nor go up into the bed that is spread for me; I will not give sleep to my eyes, nor slumber to my eyelids, until I find a place for G-D, a dwelling for the mighty one of Ya’akov.” (Tehillim 132:1–5)