Link to home
Start Free TrialLog in
Avatar of Thirt
Thirt

asked on

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.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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.

Bob
Avatar of Thirt
Thirt

ASKER

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
else...

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

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Thirt

ASKER

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

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