views:

338

answers:

2

I am designing a simple user interface using winforms. In the designer I have a panel on the form and would like to add a datagridview control into the panel at runtime and set the dock property of the datagridview to 'Fill' so that it fills the panel.

I am struggling to do this and following code is not working out for me:

Dim MyDataGridview as New DataGridView()
MyDataGridView.Dock = DockStyle.Fill
Me.MyPanel.Controls.Add(MyDataGridview)

Can anybody help point me in the right direction?

Edit:

Sorry for being vague - I don't get an error but the datagridview isn't visible. It gets added 'behind' the panel and so I tried using .SendToBack() and .BringToFront() methods thinking that the panel was hiding the datagridview but this doesn't seem to work either. At any rate, it seems like the datagridview is being added to the form but just not docked within the panel

A: 

That code should work. Is MyPanel visible and big enough?

ZippyV
+1  A: 

Set the Dock property AFTER adding the DataGridView to the panel and then call "BringToFront()" to change the Z-index.

I think that the Z-index part is what makes the trick ;)

Romias