Hide/unhide column in Gridview at runtime

Posted on 2007-08-07
Last Modified: 2013-11-26
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.
Question by:Thirt
    LVL 96

    Expert Comment

    by:Bob Learned
    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.

    LVL 4

    Author Comment

    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.

    LVL 96

    Accepted Solution

    Try something like this:

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

    LVL 4

    Author Comment

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

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

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now