This question isn't really a Christian doctrinal question, I think. But, the most obvious answer here is that they were both kings. And the literal letter of God's law is always enacted by people (like the King's army or guards), who are generally under the rule of the king -- notably as a sort of proxy for God in the case of the Jews.
So, the king probably has guards, for one. And these guards probably don't question the king's actions or authority. The people, therefore, are not likely going to form a clan and go up against the king's armed guards because he slept around. They'd just be slaughtered.
I.e., no one's going to stone the king unless God explicitly tells the people to do so via a prophet, even if the king should rightfully be stoned according to the letter of the law! And even then, the king's guards probably need to be in cahoots with the rebels, so to speak, before the people will actually take action -- or at least any successful action.
So, regardless of whether there was a good or strictly legal reason that David and Solomon shouldn't have been stoned, there's no practical reason they would have been. And there are plenty of practical de-motivators at work.