Link to home
Start Free TrialLog in
Avatar of LeighWardle
LeighWardleFlag for Australia

asked on

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?

Regards,
Leigh

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

        ' 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


Regards,
Leigh
Avatar of ashokpumca
ashokpumca
Flag of India image

Avatar of LeighWardle

ASKER

Sorry, ashokpumca, I looked through your suggested links - I cannot find a solution.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland 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
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.