The Lord said his heart was perfect and that was proven when David repented as a result of Nathan's rebuke.
I noticed in your previous post that you had answered the questions that Ehilu had asked Job but didn't answer the one that the LORD had asked the one without knowledge regarding where he was when the LORD asked "Where wast thou when I laid the foundations of the earth? declare, if thou hast understanding- when I made the cloud the garment thereof, and thick darkness a swaddlingband for it? Job had said where he was so that is one reason to consider that the question was directed to Elihu.
The Lord never said David was sinless, any insinuation to that effect is stupid.
While it is inferred by implication in 1 Sam 13:14 that "the LORD hath sought him a man after his own heart" was referring unto David, yet seeking and obtaining are two different things. "Because David did that which was right in the eyes of the LORD, and turned not aside from any thing that he commanded him all the days of his life, save only in the matter of Uriah the Hittite." 1 Kings 15:5
But I am not arguing whether or not David had a perfect heart or not since IMO that is a question that can only be answered by his creator, but I don't think that David ever stop seeking.
Again just like Job. Nathan the Prophet rebuked David, just like Elihu.
Elihu gave Job his opinion. "My words shall be of the uprightness of my heart:" Job 33:3
Elihu tells Job "Surely thou hast spoken in mine hearing, and I have heard the voice of thy words, saying, I am clean without transgression, I am innocent; neither is there iniquity in me."Job 33:8-9
Wonder what Elihu's opinion would be of someone who said the following"
20 The LORD rewarded me according to my righteousness; according to the cleanness of my hands hath he recompensed me.
21 For I have kept the ways of the LORD, and have not wickedly departed from my God.
22 For all his judgments were before me, and I did not put away his statutes from me.
23 I was also upright before him, and I kept myself from mine iniquity.
24 Therefore hath the LORD recompensed me according to my righteousness, according to the cleanness of my hands in his eyesight.
Ps 18:20-26