As far as I can tell, it comes from The Untouchables film.
According to searches at Subzin, The Untouchables was the first with this line, but has been emulated in at least 20 other films since 2000.
The Untouchables (1987)
01:21:23 Brings a knife to a gunfight.
The Target Shoots First (2000)
00:17:38 What are you doing, Max? Bringing a knife to a gunfight?
00:18:22 what are you doing, max? bringing a knife to a gunfight?
01:17:10 Who are ya, bringing a knife to a gunfight?
The Punisher (2004)
01:06:55 Bring a knife to a gunfight.
Waist Deep (2006)
00:33:12 Nigga, don't bring a knife to a gunfight, my...
BloodRayne II: Deliverance (2007)
00:28:09 Ain't it like an Irishman
to bring a bottle to a gunfight.
Dod vid ankomst (2008)
00:59:29 How typical of you, bringing a knife to a gun fight.
Indiana Jones and the Kingdom of the Crystal Skull (2008)
Nice try kid, but I think you just brought a knife. To a gunfight.
The Boondock Saints II: All Saints Day (2009)
00:35:47 That's like bringing a knife to a gunfight. / Yeah, or like bringing a really small gun to a gunfight.
The Good Guy (2009)
00:37:24 Never take a knife to a gunfight, mate.
Wonderful World (2009)
00:26:00 Never bring a knife to a gunfight.
Death Hunter (2010)
00:50:49 Just like a rookie to bring a knife to a gun fight.
Including some variations on the theme:
The Glimmer Man (1996)
00:16:59 It's kind of like takin' a screwdriver to a gunfight.
Black Cat Run (1998)
00:32:40 A crow bar to a gun fight? Drop the fucking crowbar.
Urban Justice (2007)
01:27:07 l know you ain't dumb enough to bring a fist to a gunfight.
01:12:27 [Speckles] Just like humans. Bringing guns to a space junk fight.
00:28:46 you brought a knife to a bottle fight.
00:08:06 Genius. Brings sticks to a gunfight.
The Untouchables film is based on the 1957 book of the same name, but it doesn't seem like the quote comes from the book.
The first use of the phrase I could find in Google Books is in 1989, after the film came out: in a fishing magazine and a book called Texas Wit and Wisdom). It does sound like it could be a Texas phrase.
This Ngram shows nothing before 1987:
Likewise, the first use I found in Google News is from June 24, 1987 in the Argus Press's review of The Untouchables.
'Untouchables:' It's a Brilliant Movie Destined To Be a Classic
He [Sean Connery] has lots of great lines as Ness's mentor but the best one is when he tells Ness right away the first rule of police work:
"Make sure when your shift is over you go home alive."
Another is when, fighting for his life, he cracks:
"Leave it to a ... to bring a knife to a gunfight."