Once his "house" was built and he had peace on every side, he came to realize that he was in a house, but the Ark was in a tent. And, as he did when he was wise, he went to God via his prophet Nathan and asked permission to begin ti rectify that.
But then, the Lord came back to Nathan in a dream and clarified- He pointed out that He had never asked for a house; reviewed His history with David; then promised a bright future for David and even more so for his descendants, whom would build a temple for the Ark. So basically, as Ryrie puts it, "Your request to build me a house is denied, but I will build you a house (kingdom) that will last forever (eventually through Christ).
This is God's covenant with David (Davidic Covenant). It does not promise an uninterrupted kingdom, but an eternal one.
When David heard, he bowed down and worshiped with a beautiful, beautiful prayer:"Who am I, O Lord God, and what is my house, that Thou hast brought me this far?" David was just a shepherd, which is a lowly position to have. God chooses people from unlikely positions so that we can clearly see that the Glory is from Him. This wasn't a pampered prince who had gone to the best schools and daddy donated a library so that David received privileges. This kingdom was build by God. Full stop. No one else to credit. King Shepherd Boy shaped by a loving Father.
It's a little hard to tell, but it seems like David is heartbroken, but submitting to God's design; knowing God knows him. He sees the blessing of the distant future, but seems to want to find the words to convince God; but knows better. He settles into praise and focuses on God instead of self.
Verse 23 clearly states what so many believers miss- God redeemed Israel (and each of His New Testament priests/believers) to make a name for Himself. This isn't our story except the part we're playing in His Story.
Then he agrees to the terms set in God's Davidic covenant- "...that Thy Name may be magnified forever..." David had God's glory in his sites, not his future kingdom as the priority.