1610s, "unfit, improper," from Middle English ungeinliche, from ungein (late 14c.) "inconvenient, disagreeable, troublesome," from un- (1) "not" + gein "kind, helpful; reliable; beneficial; suitable, appropriate; convenient," from Old Norse gegn "straight, direct, helpful," from Proto-Germanic *gagina "against" (see again). Old English had ungænge "useless, vain."
c. 1200, gein, "advantage, benefit; help," c. 1300, "reward, profit, that which has been acquired" (possessions, resources, wealth), from Old French gain, gaaigne "gain, profit, advantage; work, business; booty; arable land" (12c.), from Germanic, and from Old Norse (see gain (v.)). Meaning "any incremental increase" (in weight, etc.) is by 1851. Related: Gains. The French word enfolded the notions of "profit from agriculture" and "booty, prey."
Others are reading
<a href="https://www.etymonline.com/word/gainly">Etymology of gainly by etymonline</a>
Harper, D. (n.d.). Etymology of gainly. Online Etymology Dictionary. Retrieved $(datetime), from https://www.etymonline.com/word/gainly