Link to home
Start Free TrialLog in
Avatar of handyjay
handyjay

asked on

How do I autosize my .NET windows form so my datagridview displays correct

I have a datagridview that I fill on page load in my VB.NET windows forms application.  The cells in the datagrid are set to autosize allcells.  I now want the windows form that holds the datagridview to autosize based on the size of the grid.  I set the windows form autosize = true but it is not changing size.

What am I missing?
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Does setting the Dock property of the data grid to DockStyle.Fill have the desired result?
Avatar of handyjay
handyjay

ASKER

no that does not work
Is the DataGridView the only thing on the form?
yes
You can handle the Paint and Resize events of the DataGridView, and set the form to the same height/width of the DataGridView. e.g.,


Private Sub DataGridView1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
    Me.Size = Me.DataGridView1.Size
End Sub
 
Private Sub DataGridView1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Resize
    Me.Size = Me.DataGridView1.Size
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of JulienVan
JulienVan
Flag of France 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