After years of doing the rodeo queen thing, I've gotten very very good at keeping a hat on a head.
I wouldn't use hairspray, unless it's just a cheap hat and you don't care about it. Hairspray (over time) will degrade and ruin a hat. I've got two hats that are $400+ apiece, so I am not using hairspray on them. And since they are custom-made for my head, I'm not going to flip the brim down and ruin the shape of it either.
Soooooooo....... I use bobby pins. Works great (although it can hurt you scalp a little bit).
What I do, is I squish the ends of the bobby pins so that the ends aren't "flared". Because some bobby pins have ends like this:
And I want to have ends like this: (hurts less!)
Then basically what you do is slide the bobby pin onto your hat band, making sure you get some of your hair in the bobby pin too. That's what holds it on.
The best places for the most secure fit are one in front of the ear and one behind the ear. If it's really windy out, you'll also want to have them around the back of your head too.
On a super windy day, I've sometimes had 12 bobby pins to hold my hat on. On less windy days, sometimes just 4 will do.
The part that "hurts" is if you also get your scalp scraped by the bobby pin when you are inserting it.
But no pain, no gain!