Its grammar has been explained in Le Petit Grevisse book but I can't get it. What would be the gender of the adjectives that come with "gens" in these specific sentences that have been shown in the book and why?
I'm just gonna translate in my own words what Grevisse said.
Gens is masculine in most cases. The exception is: if it is preceded by a word that has a different spelling whether it is conjugated in masculine or feminine (e.g. bon is masculine, bonne is feminine, while honnête is both masculine and feminine), then whatever comes before is conjugated in feminine, but whatever comes after stays in the masculine form.
So, for example, we say:
Toutes les vieilles gens.
Because vieux / vielle is an adjectif that takes different spelling whether it is conjugated in masculine or feminine form.
But, we say:
Quels bons et honnêtes gens.
Because what immediately precedes gens is an adjective that is written the same way in both gender, honnête, so gens stays in the masculine form. If the adjective bon was immediatly before gens, then it would have been in the feminine form.
Hope it's clearer to you now!