Hide/unhide column in Gridview at runtime

I have a dropdown control on my aspx page that I use to filter data in my gridview. I would also like to hide/unhide a column in the gridview based on the user dropdown select. Is there a way to do this. Searching for this and just find directions on how to hide the column at design time.
Using VB.
Who is Participating?
Bob LearnedConnect With a Mentor Commented:
Try something like this:

    Me.GridView1.Column(2).Visible = (Me.DropDown1.SelectedValue = "ValueX")

Bob LearnedCommented:
Bind the GridView to a DataTable.DefaultView, and set the DataTable.RowFilter to filter the data base on the DropDownList.  The column has a Visible property.

ThirtAuthor Commented:
Sorry, I didn't explain myself correctly. I have a dropdown that currently is used to filter data in the gridview. That part works fine. I have the Dropdown control used as a parameter in the gridview's select query. The user selects a value from the dropdown, presses the submit button and the gridview displays the correct data.

My problem is I can't figure out how to hide a column after the user selects a specific value in the dropdown.  If they select "ValueX", I think I can add code to the Submit button on click event to do something to the effect...
if me.dropdown1 = "valuex" then
 gridview.column(2).visible = false

At least that's what I hope I can do. But I can't find the right code to do this.

ThirtAuthor Commented:
Was missing an s in columns, but you idea works great.

Me.GridView1.Columns(2).Visible = (Me.DropDown1.SelectedValue = "ValueX")
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.