Police said the suspect was a 27-year-old who was recently released from prison where he had served a term for bank robberies.
Earlier he boasted on Facebook that he'd raised £5,000 for the event.
Polish President Andrzej Duda said he was informed "doctors succeeded in reanimating the heart of the seriously injured Mayor Pawel Adamowicz and there is hope, but his condition is very hard". He was part of the democratic opposition born in that city under the leadership of Lech Walesa during the 1980s.
European Council President Donald Tusk, a former Polish prime minister who co-founded Civil Platform and is from Gdansk, tweeted: "Let's all pray for Mayor Adamowicz".
The mayor, who has held the position since 1998, is seen as progressive has and shown support for LGBT rights and tolerance for minorities. "I pray for his return to health and full strength". "Pawel, we are with you".
The stabbing, during an event that raises money for children's hospitals, has shocked Poland, the BBC's Adam Easton in Warsaw reports.