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

How to refresh DataGridView column layout

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

  • 2
1 Solution
LeighWardleAuthor Commented:
Sorry, ashokpumca, I looked through your suggested links - I cannot find a 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.
LeighWardleAuthor Commented:
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.
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.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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