• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1803
  • Last Modified:

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

0
ovcbs
Asked:
ovcbs
  • 2
1 Solution
 
Ashish PatelCommented:
try putting the code in RowDataBound event rather than rowcreated.
0
 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now