Dance legend Gregory Hines secretly battled liver cancer for two years before he died, bravely hiding his life-and-death struggle from his friends and his cast mates on “Will & Grace.”

“He was hoping he could beat it — and he didn’t want anyone fussing over him,” a source told The ENQUIRER. “No one except his family knew how sick he was.

“He kept a brave face, and was determined to beat it.”

The 57-year-old tap icon, choreographer and actor passed away on his way to the hospital on August 9 after collapsing at his Venice, Calif., home.

The sudden death of the beloved star — who played Eric McCormack’s boss Ben Doucette on “Will & Grace” — was devastating to those who knew him.

“Will & Grace” co-star Sean Hayes told The ENQUIRER: “When news like this hits you so suddenly, you’re in a state of shock. I couldn’t believe it. I immediately thought of his family and what they must be going through. My heart and prayers are with them.”

The source said: “There was no whisper that he had anything wrong with him. He hid his condition to the end, hoping he could overcome it and keep moving on. It’s just very sad.”

Hines, whose movies include “The Cotton Club” and “Waiting to Exhale,” valiantly tried to live a normal life throughout his illness. He continued performing until just two months ago, and was planning to wed his fiancee, bodybuilder Negrita Jayde.

But his health worsened dramatically around the end of May, a close family friend told The ENQUIRER: “He was gaunt and ashen, but as weak as he was, he could still flash that amazing smile — as if nothing in the world was wrong.”

Hines had a live-in nurse in the weeks before his death and was hospitalized briefly.

The close-knit “Will & Grace” cast was deeply upset by the Tony Award winner’s sudden death, an insider revealed:

“Megan Mullally, who probably knew Gregory the best, took his death the hardest out of the group — but they all loved him.

“Debra Messing was in tears and was desperately trying to find out where she could send flowers.”