Behold, I was brought forth in iniquity; And in sin did my mother conceive me.
David recognized that he was nothing more than a meare man shaped in iniquity and conceived in sin. He, just like you and me, had sin as a legacy and needed the redemption of God's grace extended to Him. So any good thing God saw in David's heart was only there as a result of David's response to God's work in him. God's electing us for His specific purposes rests on how we respond to His love and work in us by His Spirit.