myriam myriam - 3 months ago 8
Vb.net Question

Display two different Grids in the same form not at the same time

I have two different Gridviews that I have to display in the same form but not at the same time. Display

Grid1
when the user presses
Button1
and
Grid2
when they press
Button2
.

How can this be achieved?

Answer

If this is a WinForms application then I suggest to put both grids on your form designer surface, with a minimum size to be operable, one of the two grids will have its Visible property set initially to False. Also put your buttons on the form inside a panel and dock the panel to the bottom of your form.

When the code enters the form constructor apply the Dock = DockStyle.Fill to both grids and change the Visible property of the two grids at the button click

Public Sub New()
    InitializeComponent()
    grid1.Dock = DockStyle.Fill
    grid2.Dock = DockStyle.Fill   ' This has the Visible = False
End Sub

Public Sub button1_Click(sender as Object, e as EventArgs)
     grid1.Visible = False
     grid2.Visible = True

End

Public Sub button2_Click(sender as Object, e as EventArgs)
     grid1.Visible = True
     grid2.Visible = False
End
Comments