How do I hide a gridview (item) TemplateField in VB at runtime?

I have an asp gridview with many columns, some are bound fields, some are template columns. I do not allow editing so the template columns only have the "item template". My problem is that I need to hide 1 (or more)  of these template columns based on the value of a checkbox which is outside of the gridview. I have tried the code in the included snippet with 5 being the index of the column I wish to hide but it did not work. I verified that the value returned by cbxShowRetail.checked is correct. How can I hide these template columns dynamically?
Protected Sub gvSearchResults_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSearchResults.RowCreated
        Dim IsVisible As Boolean = cbxShowRetail.Checked
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(5).Visible = IsVisible
        ElseIf e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(5).Visible = IsVisible
        End If
    End Sub

Open in new window

ovcbsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ashish PatelCommented:
try putting the code in RowDataBound event rather than rowcreated.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ovcbsAuthor Commented:
This was my first question on Experts Exchange. I decided to sign up after searching for an answer on Google for 3 hours! All the solutions I found had me placing the code in the wrong event! Thank you for your quick, simple and accurate response!
0
Ashish PatelCommented:
Welcome ovcbs, and hope you find quick and better solutions as EE.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.