How to refresh DataGridView column layout

Posted on 2012-09-17
Last Modified: 2012-09-18
Hi Experts,

I have a  DataGridView component that is visible no matter which Tab Page is selected on a Tab control.

The code below shows how I am attempting to make various columns Hidden/Visible depending on which Tab is clicked.
But the columns aren't changing.

What Method do I need to update the column layout?


    Private Sub tabControl_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles tabControl.SelectedIndexChanged
        ' Move the DataGridView to the Current Tab:

        ' Change the DataGridView somehow:
        Select Case tabControl.SelectedIndex
            Case 0

                ' configure the columns for "Properties" tab

                'Hide superfluous columns (20120918)
                Me.dgvMaterials.Columns("sglCostPerVolume").Visible = False
                Me.dgvMaterials.Columns("sglCostPerArea").Visible = False
                Me.dgvMaterials.Columns("sglCostPerWeight").Visible = False
                Me.dgvMaterials.Columns("sglWeightPerVolume").Visible = False

            Case 1

                ' configure the columns for "Costs" tab

                'Hide superfluous columns (20120918)
                Me.dgvMaterials.Columns("Modulus1").Visible = True
                Me.dgvMaterials.Columns("Poissons1").Visible = True
                Me.dgvMaterials.Columns("Modulus2").Visible = True
                Me.dgvMaterials.Columns("Poissons2").Visible = True
                Me.dgvMaterials.Columns("ShearModF").Visible = True
                 'Expose previously hidden columns  (20120918)
                Me.dgvMaterials.Columns("sglCostPerVolume").Visible = True
                Me.dgvMaterials.Columns("sglCostPerArea").Visible = True
                Me.dgvMaterials.Columns("sglCostPerWeight").Visible = True
                Me.dgvMaterials.Columns("sglWeightPerVolume").Visible = True

            Case 2
                ' configure the columns for tab 2

        End Select
    End Sub

Open in new window

Question by:LeighWardle
    LVL 5

    Expert Comment

    LVL 1

    Author Comment

    Sorry, ashokpumca, I looked through your suggested links - I cannot find a solution.
    LVL 83

    Accepted Solution

    Code that you have should work. Is the grid outside tabpanel? Did you step through the code to see what code is executing? Do you bind grid before above code? Try calling Refresh method of grid at the end.
    LVL 1

    Author Closing Comment

    Thanks, CodeCruiser.

    When I stepped through the code I realized I had some True's and False's mixed up.
    The corrected code works without calling the Refresh method of the grid at the end.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    729 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

    24 Experts available now in Live!

    Get 1:1 Help Now