Look at David's life: the circumstances of his birth were such that his own family questioned his legitimacy. He was held in so little regard that when Samuel came to anoint one of Jesse's sons as king, they didn't even bother to send for him. His life as a shepherd was not idyllic; he routinely had to fight wild animals, including a lion and a bear. Saul promised David his daughter as a reward for defeating Goliath, then reneged. David married Saul's other daughter, but it wasn't all roses after that. Saul became jealous and suspicious of David and tried to kill him, forcing David to flee. Saul gave David's wife to another man and pursued him. On several occasions, David had opportunities to harm Saul but refrained, proving he was not his enemy. Saul broke off his attacks temporarily, but ultimately resumed them. Once, David fell into enemy hands and was forced to feign madness in order to escape. On another occasion, he had to pretend to be disloyal to his own people and join the enemy in battle.
Even after David became king, things were hardly perfect. He had to suffer disloyal and self-serving officers, whom he felt he needed in order to consolidate his rule. He was involved in the scandal involving Batsheva, the wife of Uriah, which led to Uriah's death. To make matters worse, the child he conceived with Batsheva died.
Later, one of his adult children violated another one (they were half-siblings or step-siblings). The girl's brother plotted to avenge her, ultimately killing the offending sibling, but resulting in his own exile. (This incident cost David the abuse of his daughter, the murder of one son and the estrangement of another.) The exiled son was eventually allowed to return, but he staged a coup in due course, forcing David to flee Jerusalem. In order to cement his rule, the son publicly violated David's concubines who had remained behind. Meanwhile, David suffered insults and indignities from parties that were loyal to Saul and resented David as king.
Upon returning to the throne, David had to put down another rebellion. Even on his deathbed, David had to deal with a son trying to usurp the throne from his chosen successor. From cradle to grave, David's life was one of conflict and strife. And how did David deal with it? Not by cursing his lot in life. No, he wrote Psalms. When things were tough, he asked G-d for help. And when things were good, he didn't forget to thank G-d.
Why was David chosen to father the eternal (and ultimately Messianic) dynasty? Not because he was perfect. David was human and he made mistakes. The difference was in how he handled those mistakes. When Saul was rebuked by the prophet Samuel, he got defensive and tried to justify his actions. This cost him the throne. Conversely, when David was rebuked by the prophet Nathan, he immediately recognized his guilt and sought to make amends. Submission to G-d does not mean that one is infallible; it means that one recognizes that G-d is always right and sublimates one's own desires upon the realization that they are in conflict with G-d's.
This is the secret of Tehillim. Through good times and bad, David always remembered to praise G-d. We always say things like "this, too, is for the good" and "just as we praise G-d for the good, so should we praise Him for the bad." In Psalms, we see that David exemplified these ideals in the fullest.